【问题标题】:collection view showing less number of cell in cellForItemAt集合视图显示 cellForItemAt 中的单元格数量较少
【发布时间】:2020-04-21 11:42:56
【问题描述】:

我有一个包含 5 个项目的数组的集合视图,如下所示:

var photos = [String]()
 override func viewDidLoad() {
        super.viewDidLoad()
 self.photos = ["background", "living", "bedroom", "dining","bathroom"]
}

在集合视图中

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

        print("total no of photos are",photos.count)
        return photos.count

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

        let cell = photocollview.dequeueReusableCell(withReuseIdentifier: "Cell10", for: indexPath) as! PhotoCollectionViewCell


        cell.layer.borderColor = UIColor.black.cgColor
        cell.layer.borderWidth = 1.0


        let str = photos[indexPath.row]

        print("str is",str)

}

集合视图中部分中的项目数显示为 5,但在 cellforitemat 中

str 打印时只显示 3 个值。

可能是什么问题?

【问题讨论】:

  • return cell ?另外,你有多少“空间”?如果只能显示 3 个单元格,则并非所有 indexPaths 都会在 cellForItemAt 中调用。
  • return cell is there.how to see all the cells?
  • 单元格大小是多少? collectionView 的大小是多少?滚动,他们会触发吗?想象一下,您有一个与collectionView 大小相同的单元格,即屏幕大小。所以 cellForItemAt 只会被触发以显示第一个单元格。这都是关于资源的优化,只在需要时调用 cellForItemAt。

标签: ios swift uicollectionview cell


【解决方案1】:

调用也返回,也许它有帮助。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   let cell = photocollview.dequeueReusableCell(withReuseIdentifier: "Cell10", for: indexPath) as! PhotoCollectionViewCell
   cell.layer.borderColor = UIColor.black.cgColor
   cell.layer.borderWidth = 1.0
   let str = photos[indexPath.row]
   print("str is",str)
   return cell
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多