Re: Reordering a table insanity
Re: Reordering a table insanity
- Subject: Re: Reordering a table insanity
- From: Graham Cox <email@hidden>
- Date: Wed, 28 Nov 2012 15:11:18 +1100
On 28/11/2012, at 2:17 PM, Graham Cox <email@hidden> wrote:
>>
>> I assume you're using begin/endUpdates around this loop to have a single animation for the whole thing? In that case, it'd be a lot easier to use removeRowsAtIndexes/insertRowsAtIndexes, since the only index that needs to be adjusted is targetIndex (it needs to be decreased by the number of indexes in items that are less than the original targetIndex). That is, you would adjust targetIndex after the removal and before constructing the index set for the insertion.
>
> Yes, I'm bracketing this with begin/endUpdates.
>
> I'll try that approach, it does sound easier. Thanks.
OK, I have now implemented it this way, and it works, in terms of ordering the items correctly.
However, I now remember why I didn't do it this way in the first place ( I did try it this way once before, when I first wrote the code a while back). The animation doesn't work well doing it this way, and the selection of the items is lost.
-[NSTableView moveRowAtIndex:toIndex:] performs a nice "swap" animation which I like and want (and tracks the selection for you). The removal/insertion approach does not produce a similar animation, or even any, despite having a set of NSAnimationOptions. That seems to be the case whether I bracket with begin/endUpdates or not. That strikes me as wrong - I should see an animation here, no? So I must be doing something wrong.
Maintaining the selection is not really an issue, as I can easily restore it, but I would like this to animate as intended. Any ideas?
Here's the relevant code. An ivar mTableDraggingRows is the index set of the items being moved, which is the selection when the drag started.
- (BOOL) tableView:(NSTableView *) aTableView
acceptDrop:(id < NSDraggingInfo >)info
row:(NSInteger) row
dropOperation:(NSTableViewDropOperation) operation
{
[aTableView beginUpdates];
NSInteger adjustment;
adjustment = [mTableDraggingRows countOfIndexesInRange:NSMakeRange(0,row)];
[aTableView removeRowsAtIndexes:mTableDraggingRows withAnimation:NSTableViewAnimationEffectFade | NSTableViewAnimationSlideUp];
NSIndexSet* insertions = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange( row - adjustment, [mTableDraggingRows count])];
[aTableView insertRowsAtIndexes:insertions withAnimation:NSTableViewAnimationEffectGap | NSTableViewAnimationSlideDown];
// perform a similar set of operations on the array holding the controllers
NSArray* itemsToMove = [mTraceControllers objectsAtIndexes:mTableDraggingRows];
[mTraceControllers removeObjectsAtIndexes:mTableDraggingRows];
[mTraceControllers insertObjects:itemsToMove atIndexes:insertions];
// adjust the managed views so they're laid out in the right order
[tracesView arrangeTraceViewsWithAnimation:YES];
[aTableView reloadData];
[aTableView endUpdates];
mTableDraggingRows = nil;
return YES;
}
--Graham
P.S. it would be really great if Corbin weighed in on this one ;-)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden