开发者

Saving reorder of tableview - Ray Wenderlich tutorial

开发者 https://www.devze.com 2023-02-15 06:30 出处:网络
I am trying to modif开发者_如何学运维y the tutorial Ray Wenderlich has created at: http://www.raywenderlich.com/1888/how-to-create-a-simple-iphone-app-tutorial-part-33

I am trying to modif开发者_如何学运维y the tutorial Ray Wenderlich has created at: http://www.raywenderlich.com/1888/how-to-create-a-simple-iphone-app-tutorial-part-33 So that I can reorder the tableview and save the new order of the bugs, but I am having trouble getting it to work.

I know I have to use the:

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

Method, but I am having a hard time figuring out what to do!

I have tried asking the question to Ray directly, but got no answer. If anyone can help me with this, I will be so happy :-)

UPDATE:

I have solved the part where I edit the _bugs array using:

NSString *stringToMove = [[_bugs objectAtIndex:fromIndexPath.row] retain]; 
[_bugs removeObjectAtIndex:fromIndexPath.row]; 
[_bugs insertObject:stringToMove atIndex:toIndexPath.row]; 
[stringToMove release];

But I am still having trouble renaming the individual files, or what I have to do, to get the _bugs to load in the right order.

Can anyone help me?


in order to reorder a table view you should first reorder the source of data where it gets populated from, and then reload part or the whole table view. Normally this data is stored within an array, and gets retrieved in the method cellForRowAtIndexPath.

In the case of the tutorial you mentioned, the array containing the data seems to be _bugs. If you look at how he deletes a row, in the tableView:commitEditingStyle:forRowAtIndexPath he first deletes the element in the array, then the row in the table view.

So, to sum up, what you should do when you want to reorder a table view is:

  1. Reorder the array: Here you can see how to achieve that.
  2. Call the method [tableView reloadData] so the tableView refreshes its data.

I hope this helped you,

cheers


Instead of using removeobject and then replacing it again try using this...'

[translations exchangeObjectAtIndex:fromindex withObjectAtIndex:toindex];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号