在CollectionView中执行批量更新期间未释放内存

问题描述 投票:0回答:1

我一直在研究一个使用两个UICollectionView的项目,那就是MainViewController,其中全屏单元格可以水平滚动,另一个可以放在全屏幕单元格上并垂直滚动。我有一个功能,可以添加和删除单元格。当用户点击并使用下面的代码删除MainViewController的其中一个页面时,仍会保留添加单元格时增长的内存。我做错了吗?我的所有代表都是弱引用,我的单元格都没有自引用闭包。我做错了什么,谢谢你的帮助

这是完整的项目

https://github.com/TheRedCamaro30/Leaky-Nested-UICollectionViews

添加或删除Cell Delegate

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
}

设置CollectionView

 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
 }
ios memory-leaks automatic-ref-counting collectionview retain-cycle
1个回答
0
投票

要插入,删除或移动单个部分或项目,您必须执行以下步骤:

  1. 更新数据源对象中的数据。
  2. 调用集合视图的相应方法以插入或删除节或项。

只需用以下代码替换removeCell方法即可。

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()
    }
}

请参考以下参考资料。

Inserting, Deleting, and Moving Sections and Items

Collection View Programming Guide for iOS

© www.soinside.com 2019 - 2024. All rights reserved.