Swift 的新手。我有一个来自一个 View Controller 的嵌套 CollectionView。主视图 Controller 有 7 个 collectionviewcell(下面代码中的“Level1Cell”)。每次单击按钮或触发事件时,我都希望 collectionView 重新加载新数据。
func eventHandler() {
// updates data
myCollectionView.reloadData()
}
然后,在它调用重新加载之后,它将在每个嵌套的 CollectionViewCell 上再次调用重新加载。
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Level1Cell", for: indexPath) as! Level1Cell
cell.appsCollectionView.reloadData()
return cell
}
问题是,假设我想为第一个单元格设置特定行的一些文本。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(self.index == 0 && indexPath.row == 30){
rightCell.textLabel.text = "asdasd"
}
第四个“Level1Cell”单元格以某种方式将其标签设置在第 30 行,但不是第二和第三行。通过调试器后,我意识到单元格在重新加载后,第四个单元格“Level1Cell”被设置为与第一个单元格具有相同的内存地址(为什么重新加载会这样做 - 它不应该为每个“Level1Cell”? - 我怎样才能解决这个问题)。另外,我不应该使用重新加载来更新 View Controller 中的 View 和嵌套 View 中的数据吗?
谢谢!
最佳答案
UIcollectionview
将重用单元格。您必须在方法中为行提供所需的数据
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
或者只是清除单元格中以前的数据。
关于ios - Swift - 单独的 View 有共享相同内存的 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45540012/