Iphone 按字幕排序tableview单元格

Iphone 按字幕排序tableview单元格,iphone,objective-c,cocoa-touch,uitableview,mapkit,Iphone,Objective C,Cocoa Touch,Uitableview,Mapkit,我得到了像这样的两点距离 [userLocation distanceFromLocation: annotationLocation] / 1000; 并将其设置为tableview的副标题,如下图所示 问题是,我可以按距离(副标题)订购这张桌子吗 谢谢 对于糟糕的english=x,很抱歉,您可以按任何方式对UITableView的单元格进行排序,但在创建表的数据源时,必须在显示单元格之前进行排序。如果使用NSFetchResultsController,可以将距离作为排序描述符。如果您

我得到了像这样的两点距离

[userLocation distanceFromLocation: annotationLocation] / 1000;
并将其设置为tableview的副标题,如下图所示

问题是,我可以按距离(副标题)订购这张桌子吗

谢谢


对于糟糕的english=x,很抱歉,您可以按任何方式对UITableView的单元格进行排序,但在创建表的数据源时,必须在显示单元格之前进行排序。如果使用NSFetchResultsController,可以将距离作为排序描述符。如果您使用的是简单的NS(可变)数组,请在将其作为表源之前对其进行排序

就像克里斯说的,你可以用

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];

用这些距离做一个数组,按你想要的顺序排列,然后

- (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;
}
好吧,像这样的事情应该能奏效


希望它有助于创建NSSortDescriptor对行进行排序:

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

假设某些对象持有一个坐标,并将这些对象放入数组位置,则可以通过执行以下操作来使用比较器块:

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;
    }
}
locations=[使用比较器排序的位置:^(id a、id b){
CLLocationDistance dist_a=[[a objectsForKey:@“坐标”]distanceFromLocation:userPosition];
CLLocationDistance dist_b=[[b objectsForKey:@“坐标”]distanceFromLocation:userPosition];
if(距离a<距离b){
返回(NSComparisonResult)或删除;
}否则如果(距离a>距离b){
返回(NSComparisonResult)或撤销;
}否则{
返回(NSComparisonResult)命令名;
}
}

您将此代码添加到
视图将显示:
以在每次显示tableView时获取更新的位置。

但annotationLocation不在数据库中。我有lat和long。在我的CD数据库中,我只有纬度和经度。当前位置按日期排序,如下所示:`[请求设置端口描述符:[NSArray arrayWithObject:[NSSortDescriptor SortDescriptor WithKey:@“date”升序:是]];self.dates=[self.managedObjectContext executeFetchRequest:请求错误:&错误]“道格拉斯,你最终使用了哪种解决方案?你使用的是CoreData吗?ferostar解决方案对我很有用。我使用的是.plist文件。userlocation和annotationLocation都是CLLocation。所以我使用-initWithLatitude:经度:和-distanceFromLocation:来获取距离并在NSArray上排序。也许最好是聊天,但我不知道使用lat和long保存位置数组。我计算从userLocation到每个位置的距离。因此我拥有所有数据,那么我在新数组中放置了什么?这里: