Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order the tableview cell by the subtitle

i'm getting the distance of 2 points like this

[userLocation distanceFromLocation: annotationLocation] / 1000;

and setting this to the subtitle of a tableview like the image bellow screenshot

the question is, can i order this table by the distances (subtitle)?

Thanks!

and sorry for the bad english =x

like image 399
Douglas Ferreira Avatar asked Jan 11 '12 14:01

Douglas Ferreira


1 Answers

You can order your UITableView's cells any way to want to, but you have to do it before showing them, when you create the table's data source. If you use an NSFetchResultsController, you can put the distance as the sort descriptor. And if you are using a simple NS(Mutable)Array, sort it before making it the table's source.

Like Chris said, you can do it with

NSSortDescriptor *titleSorter= [[NSSortDescriptor alloc] initWithKey:@"annotationLocation" ascending:YES];

If it is an NSArray what you are using, then:

[arrayOfObjects sortUsingDescriptors:[NSArray arrayWithObject:titleSorter];

and if it is an NSFetchResultsController:

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:titleSorter, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
like image 81
ferostar Avatar answered Dec 29 '22 10:12

ferostar