UITableViewDiffabledatasource NSFetchedResultController: как обновить в случае изменения - PullRequest
0 голосов
/ 09 апреля 2020

Я настраиваю tableView с новым UITableViewDiffabledatasource и NSFetchedResultController.

Вставка и удаление корректно обрабатываются из коробки. Но когда элемент обновляется (как в одном из его свойств, обновляется), и ячейка, отображающая этот элемент, должна обновляться, он не обновляется.

Как я могу убедиться, что UITableViewDiffabledatasource видит это изменение и запускает refre sh ячейки?

Добавление кода, который я использую:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = dataSource
}

func makeDataSource() -> UITableViewDiffableDataSource<String, NSManagedObjectID> {
    let reuseIdentifier = String(describing: RegisterTableCell.self)

    let dataSource =  UITableViewDiffableDataSource<String, NSManagedObjectID>(
        tableView: tableView,
        cellProvider: {  tableView, indexPath, objectID in
            let cartItem = try! self.container.viewContext.existingObject(with: objectID) as! CartItem
            let cell = tableView.dequeueReusableCell(
                withIdentifier: reuseIdentifier,
                for: indexPath
            ) as! RegisterTableCell

            cell.count.text = String(format: "%@", cartItem.count ?? "0")
            cell.label.text = cartItem.name
            cell.price.text = self.priceFormatter.string(from: NSNumber(value: cartItem.totalPrice()))
            return cell
        }
    )
    dataSource.defaultRowAnimation = .left
    return dataSource
}


func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {
    updateUI()
    let diffableSnapshot = snapshot as NSDiffableDataSourceSnapshot<String,NSManagedObjectID>
    dataSource.apply(diffableSnapshot, animatingDifferences: true, completion: nil)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...