更新单个 TableView 单元格标签

Update a Single TableView Cell Label

我正在尝试创建一个带有 'count' 变量的单元格,该变量是一个显示文件夹中有多少项目的标签。我正在使用 cellForRowAt 函数中的获取请求来执行此操作,该函数计算请求中的项目。我面临的问题是,每次添加新项目时都不会调用它,从而导致计数错误。每次在 viewWillAppear 中重新加载 tableView 可以解决问题,但在我的用例中不起作用,因为我正在处理大量数据。我该如何解决这个问题?

class FolderTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "folderCell", for: indexPath) as! FolderTableViewCell

        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Note")
        request.predicate = NSPredicate(format: "userDeleted = false")
        let count = try! context.count(for: request)


        let folders = folderNames(name: "TEST", icon: "TestImage", count: count)
        cell.updateFolders(with: folders)
        return cell
    }

}

class FolderTableViewCell: UITableViewCell {

    func updateFolders(with folders: folderNames) {
        folderCellLabel.text = folders.name
        folderCellImage.image =  UIImage(named: "TestImage")

        if folders.count != 0 {
            folderCellCount.text = "\(folders.count)"
        }
    }

}
tableView.reloadRows(at: [selectedIndex], with: .automatic)

selectedIndex 是您要重新加载的索引。

参考这个:Is it possible to refresh a single UITableViewCell in a UITableView?

谢谢