Reorganize model array to reflect table row drag and drops

58 Views Asked by At

I've been racking my brain trying to figure out a way to reorganize my model array when the user commits a drag and drop on an NSTableView. They can currently select one or more row, and drop them before or after any row in the table.

I'm given an IndexSet containing the rows of the table that were dragged. I'm also given a single destinationRow representing the destination of where those rows will be placed.

So for example, the original model looks like this: [0, 1, 2, 3, 4, 5]

And the user drags rows 1 and 2 to after 3...

Then I need to update my array to look like this: [0, 3, 1, 2, 4, 5]

Any ideas on how to efficiently achieve this? Thank you.

1

There are 1 best solutions below

4
On

I realized I'd been approaching the problem the wrong way.

I was trying to think of how to swap elements with their indexes, when all I really needed to do was use the built in functions .remove(at:) and .insert(obj, at:)

That allowed me to reorganize the array quite simply.