views:

352

answers:

1

Hi,

I use coredata to fill an array with data that is displayed in a tableview. In the tableview, I have two section. When a cell is pushed in section1, I want that cell to be moved to section2, and the other way around.

Im not quite sure how to accomplish this, and I been sitting trying to figure it out for about 8 hours now.

This is what I got so far:

I use this code to get the data:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];

NSMutableArray *sortedIngredients = [[NSMutableArray alloc] initWithArray:[event.tags allObjects]];
[sortedIngredients sortUsingDescriptors:sortDescriptors];
self.tagsArray = sortedIngredients;

[sortDescriptor release];
[sortDescriptors release];
[sortedIngredients release];

In didSelectRowForIndexPath, I figure out how to remove the cell:

Tag *tag = [tagsArray objectAtIndex:indexPath.row];

NSManagedObjectContext *context = event.managedObjectContext;
[context deleteObject:tag];

[tagsArray removeObject:tag];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

How do I insert this cell in section 2 in the tableView? Should I create 2 arrays? I tried some code that used NSDictiorary, like this:

[tableView addObject: sectionOneObject forKey:@"Section1"];
[tableView addObject: sectionTwoObjects forKey: @"Section2"];

But I never manage to get it to work.

Any help would be highly appreciated! Thanks in advance

A: 

Check out -insertRowsAtIndexPaths:withRowAnimation, e.g.:

unsigned _insertionIndices[2] = {1,0}; // second section, first row
NSIndexPath *_insertionIndexPath = [[NSIndexPath alloc] initWithIndexes: _insertionIndices length:2];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:_insertionIndexPath] withRowAnimation: UITableViewRowAnimationRight];
[_insertionIndexPath release];
Alex Reynolds