【问题标题】:Memory is not released during perform batch updates in CollectionView在 CollectionView 中执行批量更新期间未释放内存
【发布时间】: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


【解决方案1】:

要插入、删除或移动单个部分或项目,您必须遵循 这些步骤:

  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

【讨论】:

  • 感谢您的回复,我尝试进行更改以符合推荐的做法,但我仍然注意到使用批量更新删除单元格后内存没有下降。
猜你喜欢
  • 2019-02-18
  • 2021-12-28
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
相关资源
最近更新 更多