0

所以我正在尝试学习如何使用UITableViewDiffableDataSource和将单元格移动到不同的部分时遇到问题。现在我只是专注于将单元格插入朋友部分。问题是应用程序在设置为 true.apply(snapshot, animatingDifferences: animatingDifferences)时崩溃。animatingDifferences当它设置为 false 时,它​​不会崩溃,但这不会给用户在新部分中插入单元格时带来令人愉悦的效果,它会产生一种我不想使用的“活泼”效果。

这是我得到的错误:

“无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (4) 必须等于更新前该节中包含的行数 (4),加上或减去从该部分插入或删除的行数(0 插入,0 删除)加上或减去移入或移出该部分的行数(1 移入,0 移出)。

我查看更新后该部分中的行数,它确实返回了正确的新值但仍然崩溃?

现在我的 DiffableDataSource 如下:

    /* Used to edit are cells but also configures are cell on first launch */
private lazy var tableViewDataSource: EditEnabledDiffableDataSource = {
    let dataSource = EditEnabledDiffableDataSource(tableView: tableView) { [weak self] tableView, _, contact in
        
        guard let detailTableViewCell = tableView.dequeueReusableCell(withIdentifier: String(describing: DetailTableViewCell.self)) as? DetailTableViewCell else {
            return UITableViewCell()
        }
        
        if let userInfo = self?.friendsContacts.first(where: { $0.id == contact.id }) {
            detailTableViewCell.configure(with: userInfo)
        }
        if let userInfo = self?.allContacts.first(where: { $0.id == contact.id }) {
            detailTableViewCell.configure(with: userInfo)
        }
        return detailTableViewCell
    }
    
    //Updates are arrays
    dataSource.deleteClosure = { contactID in
        self.allContacts.removeAll(where: { $0.id == contactID.id})
        self.friendsContacts.removeAll(where: { $0.id == contactID.id})
    }
    
    dataSource.updateClosure = { contactID, index in
        var snap = dataSource.snapshot()
        self.friendsContacts.insert(contactID, at: index.row)
        self.configureInitialSnapshot(animatingDifferences: true)
    }
    return dataSource
}()

然后在我里面我EditEnabledDiffableDataSource有这个:

    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    super.tableView(tableView, moveRowAt: sourceIndexPath, to: destinationIndexPath)
    
    var snapshot = snapshot()
    if let sourceId = itemIdentifier(for: sourceIndexPath) {
        if let destinationId = itemIdentifier(for: destinationIndexPath) {
            guard sourceId != destinationId else {
                return // destination is same as source, no move.
            }
            // valid source and destination
            if sourceIndexPath.row > destinationIndexPath.row {
                snapshot.moveItem(sourceId, beforeItem: destinationId)
            } else {
                snapshot.moveItem(sourceId, afterItem: destinationId)
            }
        } else {
            // no valid destination, eg. moving to the last row of a section
            snapshot.deleteItems([sourceId])
            snapshot.appendItems([sourceId], toSection: snapshot.sectionIdentifiers[destinationIndexPath.section])
        }
        deleteClosure?(sourceId)
        updateClosure?(sourceId, destinationIndexPath)
    }
}

在此处输入图像描述

4

0 回答 0