在CollectionView中执行批量更新期间未释放内存 [英] Memory is not released during perform batch updates in CollectionView
问题描述
我一直在研究使用两个UICollectionView
的项目,即MainViewController,其全屏单元格可以水平滚动,而另一个位于该全屏单元格上,可以垂直滚动.我具有添加和删除单元格的功能.当用户点击并使用下面的代码删除MainViewController的页面之一时,随着添加单元格而增加的内存仍将保留.我有做错什么吗?我所有的代表都是弱引用,我的所有单元格都没有自引用闭包.我做错什么了,谢谢您的帮助
I've been working on a project of using two UICollectionView
's I have on that is the MainViewController with full screen cells that scroll horizontally and one that sits on that full screen cell and scrolls vertically. I have a functionality that adds as well as deletes cells. When a user taps and one of the pages of the MainViewController is deleted using the code below the memory that grew as cells were added is still being retained. Is there something Im doing wrong. All of my delegates are weak references and none of my cells have self referencing closures. Am I doing something wrong thank you for you help
这是完整的项目
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
}
推荐答案
要插入,删除或移动单个部分或项目,必须遵循 这些步骤:
To insert, delete, or move a single section or item, you must follow these steps:
-
更新数据源对象中的数据.
Update the data in your data source object.
调用集合视图的适当方法以插入或删除节或项.
Call the appropriate method of the collection view to insert or delete the section or item.
只需用下面的代码替换removeCell
方法.
Just replace removeCell
method with below code.
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")
//If it is not worked then reload collectionview
//self.mainCollectionView.reloadData()
}
}
请参考以下参考文献.
这篇关于在CollectionView中执行批量更新期间未释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!