OGeek|极客世界-中国程序员成长平台

标题: ios - 引用超出范围的 UICollectionViewCell [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 21:46
标题: ios - 引用超出范围的 UICollectionViewCell

我有一个包含两个单元格的 collectionView。每个单元格都适合屏幕的大小。
在第一个单元格中,我有一些按钮。
当我点击一个按钮时,collectionView 会滚动到第二个单元格,根据我点击的按钮,第二个单元格应该显示不同的 View 。

此代码用于在我点击按钮时引用第二个单元格:

if let cell = collectionView.cellForItem(at: secondIndexPath) as? SecondCell {

        // change the view of the cell
         print("Does not execute code")
}

我猜 cellForItem(at: IndexPath) 为 nil,因为单元格超出范围。
但是我该如何解决这个问题?



Best Answer-推荐答案


这个

let cell = collectionView.cellForItem(at: secondIndexPath)

当指定secondIndexPath的cell不可见时返回nil,需要修改model,再次显示时会反射(reflect)到cell , 正弦它的单元格 2 然后

let cellModel = arr[1]  // cell 2 of 0 based indexing 
cellModel.someProperty = ///

关于ios - 引用超出范围的 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54200580/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4