Order a subtitle table cell

I get a distance of 2 points like this

[userLocation distanceFromLocation: annotationLocation] / 1000;

and set it to the subtitle of the table view, as in the image below screenshot

The question is, can I order this table by distance (subtitle)?

Thank!

and sorry for bad english = x

+5
source share
4 answers

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

,

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

NSArray, :

[arrayOfObjects sortUsingDescriptors:[NSArray arrayWithObject:titleSorter];

NSFetchResultsController:

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:titleSorter, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
+6

NSSortDescriptor :

NSSortDescriptor *titleSorter= [[NSSortDescriptor alloc] initWithKey:@"annotationLocation" ascending:YES];
[arrayOfObjects sortUsingDescriptors:[NSArray arrayWithObject:titleSorter];
+1

, , ,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *_cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (_cell == nil) {
        _cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
    }
    _cell.textLabel.text = @"Transcripts";
    _cell.detailTextLabel.text = [yourArray objectAtIndex:indexPath.row];
    return _cell;
}

, - .

,

0

, , , , , :

locations = [locations sortedArrayUsingComparator: ^(id a, id b) {

    CLLocationDistance dist_a= [[a objectsForKey:@"coordinate"] distanceFromLocation: userPosition];
    CLLocationDistance dist_b= [[b objectsForKey:@"coordinate"] distanceFromLocation: userPosition];
    if ( dist_a < dist_b ) {
        return (NSComparisonResult)NSOrderedAscending;
    } else if ( dist_a > dist_b) {
        return (NSComparisonResult)NSOrderedDescending;
    } else {
        return (NSComparisonResult)NSOrderedSame;
    }
}

viewWillAppear:, , tableView.

0

All Articles