views:

480

answers:

4

I am sure this is an easy thing to do. I am very new to objective C (picked up a job the boss was going to outsource) and could use all the help I can get. Just point me in the right direction here. Basically what I want to set up is this:

I have a list of phone numbers that expands to as many rows as needed. I have no problem populating and the list works fine, but I would like for all of the recently selected items in the list to be more easily accessible at the top. So, whenever the user "didSelectRowAtIndexPath", I would like the row to move to the top.

I just KNOW this is super simple... but I haven't found the solution yet. No hurry. I am just trying to figure things out on my own. Thanks in advance!

A: 

UITableView has a method called

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

I believe you actually have to write the code though, I'm not sure because I've never worked with tables (I'm new as well).

Joe
+3  A: 

The order of the elements is the responsibility of the datasource (your code), not the table view. If you want to reorder things, then you need to reorder the data structure that you use to drive -cellForRowAtIndexPath:. If you want animations, I do not believe there's a good move animation yet. The best you can probably do (easily) is to perform a deletion and an insertion. See Batch Insertion and Deletion of Rows and Sections for details on how to animate that. A few months ago I asked the UITableView developers about the general bugginess and fragility of these animations in OS2.x, and they assured me that a lot of work went into improving that in OS3. So hopefully you can just follow the documentation and it shouldn't crash anymore.

Rob Napier
+1  A: 

There is no capability to remove a row from one location in the table to another. It can be emulated by removing the row and then adding it back in at the new location:

// Remove the row from your datasource here
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:sourcePath] withRowAnimation:UITableViewRowAnimationLeft];
// Add the row back into the datasource here
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:destPath] withRowAnimation:UITableViewRowAnimationLeft];

Replace the comments with the proper commands to update your datasource

rpetrich
A: 

Try this (the working code listing):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if( indexPath.row == 0 ) return;

NSString * selectedNum = [numbers objectAtIndex:indexPath.row];  // NSMutableArray* numbers = dataStore
[selectedNum retain];

[numbers removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
           withRowAnimation:UITableViewRowAnimationLeft];
[numbers insertObject:selectedNum atIndex:0];
[tableView insertRowsAtIndexPaths:[NSArray
           arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]
           withRowAnimation:UITableViewRowAnimationLeft];

[selectedNum release]; 

}

Tomas