UICollectionViewCell在sizeForItemAt中不可用

时间:2016-12-02 18:15:29

标签: ios uicollectionview swift3 uicollectionviewcell

我试图在集合视图中设置单元格大小更改动画。

选择了选择项目':

            collectionView.performBatchUpdates({
                collectionView.collectionViewLayout.invalidateLayout()
                let cell = collectionView.cellForItem(at: indexPath)
                let frame = cell?.frame
                cell?.frame = CGRect(x: (frame?.origin.x)!, y: (frame?.origin.y)!, width: (frame?.size.width)! + 100, height: (frame?.size.height)!)
            }, completion: nil)

应该设置单元格的大小,但没有任何反应。第二次单击该单元格,我看到单元格的大小保留为+ 100(忽略它每次都会增长的事实 - 只是试图让它立即生效)。

在sizeForItemAt函数中,任何检索单元格的尝试都返回nil。

collectionView.numberOfItems(inSection: indexPath.section)

返回3,但

collectionView.cellForItem(at: indexPath)

返回nil。为什么?

如何使用sizeForItemAt范围内的给定索引路径获取该集合视图的单元格: ???

2 个答案:

答案 0 :(得分:0)

显然sizeForItemAt在cellForItem之前被调用。

答案 1 :(得分:0)

确定indexPath.row< 3?因为如果没有那么它将返回零。在cellForItem中放置一个断点,看看会发生什么。

相关问题