4

I have an NSTableView which uses an array controller as binding to a dynamic array and it works great.

Now I realized that when I am appending / inserting an element to the array the row is directly added to the table view without the row insert animation. When I am often adding elements to the array the table view looks weird due to the missing animation.

Is there any "official" way or workaround to get the row animations when I am inserting or removing an element of the dynamic array?

Marek H
  • 5,173
  • 3
  • 31
  • 42
Sebastian
  • 8,952
  • 3
  • 32
  • 30
  • Duplicate of [Using NSTableView Animations with Bindings](http://stackoverflow.com/questions/13541613/using-nstableview-animations-with-bindings). – Willeke Mar 15 '16 at 22:23
  • 2
    The link just mentions how to remove an item but not how to insert an item. – Sebastian Mar 16 '16 at 09:30
  • 1
    It is the same trick. Instead of 'remove from tableview, remove from array' do 'insert in array, insert in tableview'. – Willeke Mar 16 '16 at 11:30
  • 3
    It is not the same trick if you use nsarraycontroller as a backing storage. The reason is that from your model has to be updated first and then one can call insert on tableview. I am having issues how to stop arraycontroller redrawing the tableview so i can do the animation – Marek H Aug 21 '18 at 10:12
  • When trying the same trick, I get an index out of bounds or the whole table redraws and moves. I think this is because I am inserting a row in the table that has no backing until the model is updated. Removing works fine, but adding does not. – Trygve Sep 07 '21 at 16:49

1 Answers1

0

I know this questions is pretty old, but I ran into a similar issue when trying to animate an insertion within a NSTableView which uses bindings. This is what I ended up doing:

  1. Unbind the array controller from the tableview content.
  2. Insert the new object into the array controller (without unbinding first this would automatically insert it into the tableview without animation).
  3. Animate the tableview insertion.
  4. Re-bind the array controller to the tableview content.
    // insertIndex and objectToInsert established earlier
    [self.myTableView unbind:NSContentBinding];
    [self.myArrayController insertObject:objectToInsert atArrangedObjectIndex:insertIndex];
    [NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) {
        [self.myTableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:insertIndex] withAnimation:NSTableViewAnimationSlideDown];
    } completionHandler:^{
        [self.myTableView bind:NSContentBinding toObject:self.myArrayController withKeyPath:@"arrangedObjects" options:@{NSRaisesForNotApplicableKeysBindingOption : @(YES)}];
    }];
R4N
  • 2,455
  • 1
  • 7
  • 9