How to move rows in a table in an iPhone application

Override the following delegate method to allow row moving.

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

return YES;


-(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{

NSUInteger rowFrom = [fromIndexPath row];

NSUInteger rowTo = [toIndexPath row];

id idObject = [[array objectAtIndex: rowFrom] retain];

[array removeObjectAtIndex: rowFrom];

[array insertObject: idObject atIndex: rowTo];

[idObject release];


Override the above delegate method to do the actual row moving.

No comments: