【发布时间】:2019-03-26 03:16:39
【问题描述】:
我一直在从事一个使用两个UICollectionView 的项目,即 MainViewController 具有水平滚动的全屏单元格和一个位于该全屏单元格上并垂直滚动的单元格。我有一个添加和删除单元格的功能。当用户点击并使用下面的代码删除 MainViewController 的一个页面时,随着单元格的添加而增长的内存仍然被保留。有什么我做错了吗。我所有的代表都是弱引用,我的单元格都没有自引用闭包。我是不是做错了什么谢谢你的帮助
这是完整的项目
https://github.com/TheRedCamaro30/Leaky-Nested-UICollectionViews
添加或删除单元委托
func addCell(){
self.mainCollectionView.performBatchUpdates({
guard let last = arr.last else{
return
}
arr.append(last + 1)
let lastIndex = IndexPath(item: last, section: 0)
self.mainCollectionView.insertItems(at: [lastIndex])
}) { (completed) in
print("Batch updates completed, performed successfully: \(completed)")
}
}
func removeCell() {
self.mainCollectionView.performBatchUpdates({
arr.popLast()
self.mainCollectionView.deleteItems(at: [IndexPath(item: arr.count - 1, section: 0)])
}) { (competed) in
print("Perform batch updates completed")
}
}
全尺寸细胞群
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FullPageCell.reuseIdentifier, for: indexPath) as? FullPageCell else{
assertionFailure("Fatal Error FullPageCell not dequed")
return UICollectionViewCell()
}
cell.backgroundColor = UIColor.green
cell.cellTappedDelegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let cell = cell as? FullPageCell{
cell.setUpCollectionView()
}
}
SubCollectionView 坐在整页单元格上
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SmallCell.reuseIdentifier, for: indexPath) as? SmallCell else{
assertionFailure("Fatal Error FullPageCell not dequed")
return UICollectionViewCell()
}
cell.backgroundColor = UIColor.yellow
cell.imageView.image = self.image
return cell
}
SetUpCollectionView
func setUpCollectionView(){
let view = self.contentView
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 1
layout.minimumInteritemSpacing = 1
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: (view.bounds.width - 2)/3, height: (view.bounds.width - 2)/3)
collectionView = UICollectionView(frame:view.bounds, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(SmallCell.self, forCellWithReuseIdentifier: SmallCell.reuseIdentifier)
collectionView.backgroundColor = UIColor.white
self.collectionView.isPagingEnabled = true
view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
【问题讨论】:
-
请添加代码以将数据填充到collectionview中。
-
@iMHiteshSurani 请查看添加的代码
-
只需注释 cell.cellTappedDelegate = self line 然后执行代码。我认为内存问题是由于委托。你没有好好管理
-
@iMHiteshSurani 感谢您一直以来的帮助。 cellTappedDelegate 是 collectionViewCell 中的弱引用。我用它来检测用户何时点击了 collectionViewCell 内的 collectionView 中的一个单元格,如果没有它,我将无法触发调用 performBatchUpdates 的 removeCell 函数。
-
setUpCollectionView() 方法是做什么的
标签: ios memory-leaks automatic-ref-counting collectionview retain-cycle