【问题标题】:UITableViewCells disappearing when scrolling on iOS 15+在 iOS 15+ 上滚动时 UITableViewCells 消失
【发布时间】:2022-06-16 09:37:08
【问题描述】:

我最近更新到运行 iOS 15.5 模拟器的 Xcode 版本 13.4.1。我注意到在 iOS 15.5 模拟器上运行我的项目时,在加载带有 uicollectionviews 的 uitableviewcells 时出现以下错误。

[Assert] UITableView internal inconsistency: cell prefetched for IP(0,6) already stored for IP(0,5).

而 uitableviewcells 似乎在滚动时偶尔会消失。但是,当我在 iOS 13 模拟器上运行它并且表格视图正确加载且没有问题时,这不会发生。它似乎发生在包含 UICollectionView 的 uitableviewcells 上。

这是代码中的一个 sn-p:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell") as? TestTableViewCell else {fatalError()}

        cell.collectionView.registerNibArray(withNames:["TestCollectionViewCell"])
        cell.collectionView.delegate = self
        cell.collectionView.dataSource = self
        cell.collectionView.reloadData()
        return cell
}

在最近的 iOS 版本中,uitableview 预取似乎发生了变化,任何解决方案都会很有帮助。

【问题讨论】:

  • 这是一个非常糟糕的方法。您应该将集合视图逻辑(注册单元、委托、数据源等)放在单元类本身中。没有看到minimal reproducible example 就无法确定,但很可能是导致您的问题的原因。

标签: ios swift xcode uitableview


【解决方案1】:
  1. 您不需要在每一行上设置委托和数据源。当 UIViewController 加载或出现时调用它。这部分代码很糟糕,但不应阻止它工作。

  2. 寄存器相同,加载时调用,而不是每一行

  3. 永远不要在 cellForRowAt 中调用 reloadData,你将在一个无限循环中结束。 reloadData 将调用 cellForRowAt 和 cellForRowAt 调用 reloadData...

这是 cellForRowAt 的基本模板:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // cell reuse id (cells that scroll out of view can be reused)
        private let CellIdentifier = "MyCellID"
        
        // create a new cell if needed or reuse an old one
        var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier)
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: CellIdentifier)
        }
        
        // set the text from the data model
        cell.textLabel?.text = "Blabla"
        
        return cell!
        
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多