iOS UICollectionViewCell中cellForItem方法不执行

1,229 阅读1分钟

情景:在UITableview的组头中有一个UICollectionView,但是UITableview有的section有组头,有的没组头,所以动态设置UICollectionView的高度,结果发现滑动一屏之后,回到刚开始滑动的地方,组头一片空白。 通过打断点分析:

1.执行


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.sectionModel?.exchangeList.count ?? 0
    }

2.不执行


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {}

解决方法:更新布局的时候,强制刷新布局


collectionView.snp.updateConstraints { make in
    make.height.equalTo(collectionViewH)
}
collectionView.reloadData()
collectionView.layoutIfNeeded()