首页 > 解决方案 > 每次出现新行时如何更新所有可见的 UITableViewCell

问题描述

我希望UITableView新单元格的出现触发 ' 刷新。

简单的设置。UITableView,dataSource[Double]. 表中的每一行将在数组中显示一个数字。

我想做的特别的事情是我希望表格用所有可见单元格的最大数量来标记单元格。然后每个其他单元格将计算与屏幕上最大数量的差异。每次出现新单元格时,这都应该更新。

它应该是什么样子的演示

import UIKit

class ViewController: UIViewController {
    var max = 0.0
    var data:[Double] = [13,32,43,56,91,42,26,17,63,41,73,54,26,87,64,33,26,51,99,85,57,43,30,33,20]
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.reloadData()
    }
    
    private func calculate() {
        // calculate the max of visible cells
        let indexes = tableView.indexPathsForVisibleRows!
        max = indexes
            .map { data[$0.row] }
            .reduce(0) { Swift.max($0,$1) }
        
        // trying to update all the visible cells.

//         tableView.reloadRows(at: indexes, with: .none) ****
//         tableView.reloadData() ****
        
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell  = tableView.dequeueReusableCell(withIdentifier: "default")!
        cell.textLabel?.text = "\(data[indexPath.row]) : \(max)"
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        calculate()
    }
    
    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        calculate()
    }
}

我做了什么

有 2 行代码,末尾带有 **** 标记。

如果我取消注释tableView.reloadRows(at: indexes, with: .none),Xcode 会产生错误:'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 15 beyond bounds [0 .. 13]'. 我真的不明白为什么,但我知道屏幕上的可见单元格最多为 14 个以适合屏幕,但在表格加载阶段,模拟器认为在某个时候有 19 个可见单元格

相反,如果我取消注释tableView.reloadData(),这行代码将触发willDisplayfunc ,它将再次调用calculate其中包含的 func reloadData()。这是一个无限递归循环,屏幕上没有成功显示任何行。

相反,如果我不取消任何注释,表格将不会更新屏幕上已经存在的单元格。只有新出现的单元格才能正确显示我想要的效果。

感谢您阅读所有这些并尝试提供帮助。

标签: iosswiftuitableviewreloaddata

解决方案


你不想打电话reloadCells,因为这会触发willDisplay,然后你会得到一个无限循环。

相反,您可以通过调用可见单元来访问可见单元并直接更新它cellForRow(at:)

private func updateVisibleCells() {
    for indexPath in self.tableView.indexPathsForVisibleRows ?? [] {
        if let cell = self.tableView.cellForRow(at: indexPath) {
            cell.textLabel?.text = "\(data[indexPath.row]) : \(max)"
        }
    }
}

现在,如果您在拨打电话updateVisibleCells后直接拨打电话calculate()willDisplayCell您将在控制台中收到一条消息:

尝试在表格视图上调用 -cellForRowAtIndexPath: 时,它正在更新其可见单元格,这是不允许的。

这将导致发布版本崩溃。

为了解决这个问题,我们可以updateVisibleCells通过异步调度它来推迟调用,以便在表视图更新完成后调用它:

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    self.calculate()
    DispatchQueue.main.async {
        self.updateVisibleCells()
    }
}

当行滚动出视图时,您希望使用didEndDisplaying而不是正确更新行。willDisplay你不需要任何东西willDisplay


推荐阅读