将动画差异设置为 true 时,使用 UITableViewDiffableDataSource 将单元格移动到新部分会导致应用程序崩溃

问题描述

所以我正在尝试学习如何使用 UITableViewDiffableDataSource 并且在将单元格移动到不同部分时遇到问题。现在我只是专注于将单元格插入朋友部分。问题是当 .apply(snapshot,animatingDifferences: animatingDifferences) 设置为 true 时,应用程序会在 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)
    }
}

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)