CollectionView方法cellForItemAt indexPath:不被调用(快速)

时间:2020-02-04 12:00:20

标签: ios swift xcode uicollectionview

我使用过UICollectionViewController,并且试图将图像添加到我的收藏夹视图单元格中,但是未调用UICollectionView方法。

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! CircularCollectionViewCell
        cell.imageName = images[indexPath.row]
        return cell

    }

4 个答案:

答案 0 :(得分:3)

提示:如果在重新加载之前更改collectionView的大小或位置,则不会调用 CellforRow at:indexPath 方法,请确保您没有这样做。

答案 1 :(得分:0)

self.collectionView.reloadData()

答案 2 :(得分:0)

UICollectionViewController的子类delegatedataSource中默认设置。

默认情况下,数据源方法中使用0。您需要更改这些值。

override func numberOfSections(in collectionView: UICollectionView) -> Int {        
    return 1
}    

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {        
    print(images.count)
    return images.count
}

答案 3 :(得分:0)

如果您不向集合视图提供内容大小信息,则不会调用 cellForItemAtIndexPath

您可以做的是设置collectionViewLayout,如下所示:

 let flowLayout = UICollectionViewFlowLayout()
 flowLayout.scrollDirection = .vertical
 collectionView.collectionViewLayout = flowLayout