Skip to content

Commit

Permalink
Merge pull request #144 from tobiasjordan/bugfix/NSTableView-batch-up…
Browse files Browse the repository at this point in the history
…dates

Fix batch moving of rows in NSTableView
  • Loading branch information
ra1028 authored Jun 24, 2022
2 parents 8d45ea9 + 4139c9e commit 106be19
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Sources/Extensions/AppKitExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,24 @@ public extension NSTableView {
removeRows(at: IndexSet(changeset.elementDeleted.map { $0.element }), withAnimation: deleteRowsAnimation())
}

if !changeset.elementInserted.isEmpty {
insertRows(at: IndexSet(changeset.elementInserted.map { $0.element }), withAnimation: insertRowsAnimation())
}

if !changeset.elementUpdated.isEmpty {
reloadData(forRowIndexes: IndexSet(changeset.elementUpdated.map { $0.element }), columnIndexes: IndexSet(0..<tableColumns.count))
}

for (source, target) in changeset.elementMoved {
moveRow(at: source.element, to: target.element)
if !changeset.elementMoved.isEmpty {
let insertionIndices = IndexSet(changeset.elementInserted.map { $0.element })
var movedSourceIndices = IndexSet()

for (source, target) in changeset.elementMoved {
let sourceElementOffset = movedSourceIndices.count(in: source.element...)
let targetElementOffset = insertionIndices.count(in: 0..<target.element)
moveRow(at: source.element + sourceElementOffset, to: target.element - targetElementOffset)
movedSourceIndices.insert(source.element)
}
}

if !changeset.elementInserted.isEmpty {
insertRows(at: IndexSet(changeset.elementInserted.map { $0.element }), withAnimation: insertRowsAnimation())
}

endUpdates()
Expand Down

0 comments on commit 106be19

Please sign in to comment.