0

I've made an app, which downloads a JSON list from the internet and add the JSON list to the UITableView. After the content is in the UITableView, we start looking for the user via GPS. I would like the UITableView to rearrange after distance to the row's content.

I got the distance to each rows, but I do not know to rearrange and sort it after the distance.

Any hints?

Hedam
  • 2,209
  • 27
  • 53

2 Answers2

0

I suggest to reload the table after receiving updated informations: every time you receive updated information from the GPS, you have to sort your data array by the distance.

Now that you have a new sorted array you can reload the table, that will show the cell ordered.

Marco Pace
  • 3,820
  • 19
  • 38
0

If you have an array and that array contains values that you want to sort by you can call

- (NSComparisonResult)compare:(tableDataObject *)otherObject {
    return [self.distance compare:otherObject.distance];
}

//NSArray *sortedArray;
tableData = [tableData sortedArrayUsingSelector:@selector(compare:)];

like this answer says.

Once you have your sorted array all you need to do is call -reloadData on your table and it will resort the table based on your values. (You need to make sure you sort the array into the array used by your tableview)

You can also do all of this inside a UITableViewAnimation block so that it's animated pretty like...

[myTableView beginUpdates];
[myTableView reloadData];
[myTableView endUpdates];
Community
  • 1
  • 1
Putz1103
  • 6,211
  • 1
  • 18
  • 25