【问题标题】:Update a Single TableView Cell Label更新单个 TableView 单元格标签
【发布时间】:2020-03-14 11:47:47
【问题描述】:

我正在尝试使用“计数”变量创建一个单元格,该变量是一个标签,显示文件夹中有多少项目。我正在使用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)"
        }
    }

}

【问题讨论】:

    标签: ios swift tableview


    【解决方案1】:
    tableView.reloadRows(at: [selectedIndex], with: .automatic)
    

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

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

    谢谢

    【讨论】:

    • 这是最好的方法吗?这种方法的唯一问题是不会从另一个 VC 取消选择 unwind ...
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多