1

I am having UICollectionView inside a UITableViewCell and its delegate and datasource are declared there in table cell class. It works fine for first time loading but later on changes it's size when I scroll collection. I noticed sizeForItemAt is called only while loading but not while setting the cells during scroll.

class HomeCell: UITableViewCell {

    @IBOutlet weak var collHomeSongs: UICollectionView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setCollection() {
        collHomeSongs.delegate = self
        collHomeSongs.dataSource = self
    }
}

extension HomeCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: 120, height: 180)
    }
}
qtmfld
  • 2,916
  • 2
  • 21
  • 36

1 Answers1

2

I just got it's solution this problem arises in Xcode 11+, and solution to it is set collection view automatic size to "None". Refer: Why on Xcode 11, UICollectionViewCell changes size as soon as you scroll (I already set size in sizeForItem AtIndexPath:)?

qtmfld
  • 2,916
  • 2
  • 21
  • 36
gn. Dolly
  • 76
  • 4