UICollectionView self sizing cells. Insert/reloadData breaks layout
Asked Answered
C

0

8

Writing this question as my last resort, being stuck on this for few days now. I am trying to implement self sizing cells for UICollectionView with FlowLayout. Everything seem to work fine until I reladData() or do insertItemsAtIndexPaths() while being at the bottom of the collectionView.

This is my Cell:

class FooFoo: UICollectionViewCell {

var label: UILabel!
var text: String! {
    didSet {
        label.text = text
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)

    setupView()
    setupConstraints()
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupView() {
    self.contentView.backgroundColor = UIColor.yellowColor()

    label = ({
        let view = UILabel()

        view.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.contentView.addSubview(view)

        return view
    })()
}

func setupConstraints() {
    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-100-[childView]-100-|", options: nil, metrics: nil, views: ["childView": label]))
    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-100-[childView]-100-|", options: nil, metrics: nil, views: ["childView": label]))
}

}

This is CollectionView definition:

    collectionView = ({
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 1
        layout.estimatedItemSize = CGSize(width: 300, height: 300)

        let view = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

        view.dataSource = self
        view.delegate = self


        view.registerClass(FooFoo.self, forCellWithReuseIdentifier: "Cell")

        view.backgroundColor = UIColor.clearColor()

        self.view.addSubview(view)

        return view
    })()

This is dataSource:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return items.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! FooFoo
    cell.text = "FooBar: \(indexPath.item)"
    return cell
}

After I fetch additional items, i tried both inserting new array of indexes, or reloading data and the result is the same - broken layout.

I also tried replacing estimatedItemSize with itemSize and it did work just fine, so I am pretty certain its because self-sizing cells.

This is the result:

Insert bathc of items on Fetch breaks layout

Civilian answered 12/7, 2015 at 10:29 Comment(6)
I'm dealing with the same issue now. Did you ever figure out what was going on?Embowel
Hey. I did submit a bug. Apple replied few months ago that they fixed in in iOS 9 beta 4 (Build: 13A4305g). I have not confirmed fix yet - will confirm and will update my question.Civilian
Hi @Civilian I'm having a similar issue with the reloadData, have you find a solution?Rida
Did not check yet. I did fill an Radar and some time ago received email that it will be resolved in iOS 9 or so ... did not check if it got fixed myself.Civilian
Hi there, any updates?Ianthe
@Ianthe did not check myself. Apple did reply that they fixed it in iOS 9. Might dig around might find that project somewhere on archive so I can test it.Civilian

© 2022 - 2024 — McMap. All rights reserved.