I got the impression that automatic cell calibration in UICollectionView became very easy in iOS 8. So, I probably missed something.
I use a subclass of UICollectionViewFlowLayout as my layout:
class BuildCollectionViewFlowLayout: UICollectionViewFlowLayout { required init(coder: NSCoder) { super.init(coder: coder) self.minimumLineSpacing = 1 self.sectionInset.top = 20 self.estimatedItemSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: 90) } }
Then my ViewController , which is a subclass of UICollectionViewController , looks like this:
class ViewController: UICollectionViewController { let CellIdentifier = "CellIdentifier" let apiClient: APIClient() var builds:Array<JSON>? = [] override func viewDidLoad() { super.viewDidLoad()
And finally, my view cell:
class BuildProjectStatusCollectionViewCell : UICollectionViewCell { var projectNameLabel: UILabel! var lastCommitMessageLabel: UILabel! override init(frame: CGRect) { super.init(frame: frame) self.initialize() } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.initialize() } override func awakeFromNib() { self.initialize() } func initialize() { self.contentView.frame = self.bounds; self.contentView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight self.projectNameLabel = UILabel(forAutoLayout: ()) self.lastCommitMessageLabel = UILabel(forAutoLayout: ()) self.lastCommitMessageLabel.autoresizingMask = UIViewAutoresizing.FlexibleHeight self.lastCommitMessageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping self.lastCommitMessageLabel.numberOfLines = 0 self.contentView.addSubview(self.projectNameLabel) self.contentView.addSubview(self.lastCommitMessageLabel) setNeedsUpdateConstraints() } func setup(project:JSON!) -> Void { self.projectNameLabel!.text = project["name"].stringValue let builds: Array<JSON> = project["builds"].arrayValue! if builds.count == 1 { if builds[0]["status"].stringValue == "success" { self.backgroundColor = UIColor(red: 68/255, green: 175/255, blue: 105/255, alpha: 1.0) } else { self.backgroundColor = UIColor(red: 254/255, green: 57/255, blue: 48/255, alpha: 1.0) } self.lastCommitMessageLabel!.text = builds[0]["message"].stringValue } } override func preferredLayoutAttributesFittingAttributes(layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes! { let attr: UICollectionViewLayoutAttributes = layoutAttributes.copy() as UICollectionViewLayoutAttributes
Cells get the size specified as estimatedItemSize in the layout class.
Should I do a manual calculation of the element height in the preferredLayoutAttributesFittingAttributes method?
If so, aren't the tables showing the tables 100% of their own sizes? ( http://www.appcoda.com/self-sizing-cells/ )
source share