【发布时间】:2018-01-14 08:27:49
【问题描述】:
Swift 新手。我有一个来自一个视图控制器的嵌套 CollectionView。主视图控制器有 7 个collectionviewcell(下面代码中的“Level1Cell”)。每次单击按钮或触发事件时,我都希望 collectionView 重新加载新数据。
func eventHandler() {
// updates data
myCollectionView.reloadData()
}
然后,在它调用 reload 之后,它会在每个嵌套的 CollectionViewCell 上再次调用 reload。
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”? - 我怎样才能解决这个问题)。另外,我不应该使用重新加载来更新视图和视图控制器中的嵌套视图中的数据吗?
谢谢!
【问题讨论】:
标签: ios iphone swift memory uicollectionview