Swift CollectionDifference 的轻量级扩展,除了删除和插入之外,还支持移动

差异跟踪器

DifferenceTracker 是 Swift 的轻量级扩展。它定义了 除了删除和插入之外,移动在更新接口和管理时至关重要 引用类型。CollectionDifference

DifferenceTracker可用于在/呼叫上和呼叫之间执行动画更新。这当前不支持更新 ,或在呼叫中, 这需要微妙不同的排序。NSTableViewsNSOutlineViewsbeginUpdates()endUpdates()UITableViewUICollectionViewNSCollectionViewperformBatchUpdates()

DifferenceTracker 生成一系列步骤,这些步骤可以迭代地应用于接口,或者 关联的集合,用于移动、插入和删除元素。除了第一步和最后一步, 所有步骤索引都是暂时性的,与开始或结束集合没有直接关系。

示例用法

outlineView.beginUpdates()
for step in collectionDifference.steps {
    switch step {
    case let .remove(_, index):
        outlineView.removeItems(at: [index], inParent: node, withAnimation: animation)
                    
    case let .insert(element, index):
        outlineView.insertItems(at: [index], inParent: node, withAnimation: animation)

    case let .move(element, from, to):
        outlineView.moveItem(at: from, inParent: node, to: to, inParent: node)
    }
}
outlineView.endUpdates()

确认

DifferenceTracker的开发得到了@paxos和他的全面测试套件的帮助。谢谢。

GitHub

查看 Github