MrDatabase
MrDatabase

Reputation: 44425

Sorting rows in UITableViewController

I have a UITableViewController containing some very basic data. Assume each row just contains a random integer. I'd like to sort these rows (either ascending or descending is fine). I have a "Sort" button and a "sortItems" delegate that's called when the Sort button is tapped. How do I sort the contents of the UITableViewController and update the display? I also store the data displayed in the UITableViewController in an array if that helps.

Cheers!

Upvotes: 1

Views: 587

Answers (2)

PengOne
PengOne

Reputation: 48398

Assuming you have integers saved in an array, sort it, and then use it to fill the tableview by customizing

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 

to populate cell i with sortedInts[i].

Upvotes: 1

sergio
sergio

Reputation: 69027

The data source associated to your UITableViewController is the main access point to the data you display in your cell.

How don't know how your data model looks like, but it could be as simple a matter as sorting an array of integer (in your example) store in your data source.

When I say "data source" I mean: the object that is implementing the UITableViewDataSource Protocol

So, sort your model and then reloadData in your tableViewController.tableView.

Upvotes: 2

Related Questions