Reputation: 253
Following on from my last question, I have a tableview populated with 2 NSMutableArrays, 2 sections and 2 section headers, all of which are working ok.
incompleteItems = [NSMutableArray arrayWithObjects: @"Item 1", @"Item 2", @"Item 3"... , nil];
completeItems = [NSMutableArray arrayWithObjects: nil];
I have set it up so that when a user swipes to the right, the cell accessory changes from disclosure to checkmark, and when swiping to the left changes from checkmark to disclosure. The gestures are achieved using handleSwipeLeft/Right methods.
What I'd like to do is when a user swipes to the right, the Item from the incompleteItems array is moved into the completedItems array, thus moving the item from the "Incomplete" section to the "Complete" section.
What would be the best way to achieve such a thing? My experience is limited so this is somewhat outside of my knowledge.
Any help appreciated as always.
Upvotes: 1
Views: 158
Reputation: 1485
Do you need a nice animation? Check out: deleteRowsAtIndexPaths:withRowAnimation:
and insertRowsAtIndexPaths:withRowAnimation:
. Make sure you wrap these calls with beginUpdates
and endUpdates
.
I don't know of an easy way to animate moving a cell from one tableView to another. I can imagine a method that requires you to do a 3-step animation manually, removing the cell (and adding it to a new view that floats in front of your whole view hierarchy), moving the new view to the destination location (you may need to do your own math to determine the screen coordinates of the destination) and finally an addition animation. You could use a dummy cell that is completely transparent and swap it in / out in the first and last pieces of the animation to give the tableview something to hold the place of your transferred cell.
Upvotes: 0
Reputation: 57169
You can simply add the object from one array to the other and then delete it immediately.
-(void)incompleteToCompleteAtIndex:(NSUInteger)index
{
[complete addObject:[incompleteItems objectAtIndex:index]];
[incompleteItems removeObjectAtIndex:index];
}
-(void)completeToIncompleteAtIndex:(NSUInteger)index
{
[incompleteItems addObject:[complete objectAtIndex:index]];
[complete removeObjectAtIndex:index];
}
Since this will only occur as a result of a UI action I assume that this will happen on the main thread and there will be no reason for locking.
Upvotes: 1