0

I have a CollectionView setup and i have a button inside that increases the cell height by a fixed amount. However when it does this is overlapps with the cell below meaning the cell below doesnt move down to make space.

Ive been trying to figure this out for days and i cant seem to get it working.

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var colors = [UIColor.red,UIColor.blue]
    @IBOutlet weak var col: UICollectionView!

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! cellCollectionViewCell
        cell.frame.size.height = CGFloat(317 + cell.button.tag*45)
        cell.backgroundColor = colors[indexPath.row]
        return cell
    }

    @IBAction func increaseHeight(_ sender: Any) {
        col.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

class cellCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var butt: UIButton!
    @IBAction func incHeight(_ sender: UIButton) {
        sender.tag = sender.tag + 1
    }
}


enter image description here enter image description here

enter image description here

Kevin DiTraglia
  • 24,092
  • 16
  • 88
  • 134
nazorbeam
  • 23
  • 4

1 Answers1

0

Editing the frame of the cell directly is probably not the best approach. Instead override collectionView sizeForItemAtIndexPath, and specify the height change there. This should allow the collection view to re-layout correctly with the new cell height.

You can see other questions like this one for more details

Kevin DiTraglia
  • 24,092
  • 16
  • 88
  • 134