【发布时间】: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)"
}
}
}
【问题讨论】: