views:

343

answers:

1

I am trying to insert a new cell into a table view where the data is sorted. Is there an easy & efficient way to do this using the API?

The easiest way seems to be insert the new data into an NSMutableArray, sort using a sort descriptor, and call reloadData. This should only redisplay the visible rows, so it doesn't look too bad.

Would it be worthwhile to do a binary search on the data array, insert the data and then insert the cell?

+2  A: 

It really depends on how many rows are in your table. If your table only has around 10 rows, simply adding the data to your NSMutableArray, resorting, and reloading the UITableView should be just fine.

On the other hand, if your table has hundreds or thousands of rows, you are definitely going to want to do something more efficient. Using a binary search to insert your data into your NSMutableArray is a good idea. Then all you have to do is call UITableView's insertRowsAtIndexPaths:withRowAnimation: to add the row to the actual table view. No reloading required.

Sebastian Celis