2012-01-11 30 views
5

我得到的2个点的距离,这样为了通过字幕

[userLocation distanceFromLocation: annotationLocation]/1000; 

和波纹管 screenshot http://i41.tinypic.com/ruxv74.png

问题是这样的设置,如图像的tableview的字幕中的tableview细胞,我可以通过距离(小标题)订购这张表吗?

谢谢!

和遗憾的英语不好= X

+0

道格拉斯,你最终使用哪种解决方案?你在使用CoreData吗? – marciokoko 2013-02-13 02:09:58

+0

ferostar解决方案适合我。我正在使用.plist文件。 userlocation和annotationLocation都是CLLocation。 所以我使用了-initWithLatitude:longitude:和-distanceFromLocation:来获取距离并将其排序在NSArray上。 – 2013-02-13 15:43:54

+0

也许这是最好的聊天,但我有拉特和长的位置阵列。我计算从userLocation到每个位置的距离。所以我拥有所有的数据,那么我应该如何放置新阵列?在这里:http://stackoverflow.com/questions/14845434/how-do-i-sort-table-cells-from-core-data – marciokoko 2013-02-13 15:57:40

回答

6

你可以为了你的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]; 
+0

完美! 它对我来说非常好。 谢谢大家=] – 2012-01-11 17:54:29

+0

但annotationLocation不在数据库中。我有拉特和长。 – marciokoko 2013-02-11 15:52:25

0

用这些距离制作一个阵列,按照你的要求排列它

- (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

在我的CD数据库中,我只有经度和纬度。目前,位置按如下排序:' [请求setSortDescriptors:[NSArray arrayWithObject: [NSSortDescriptor sortDescriptorWithKey:@“date”ascending:YES]]]; self.dates = [self.managedObjectContext executeFetchRequest:request error:&error]; ' – marciokoko 2013-02-12 22:09:16

1

创建NSSortDescriptor来排序行:

NSSortDescriptor *titleSorter= [[NSSortDescriptor alloc] initWithKey:@"annotationLocation" ascending:YES]; 
[arrayOfObjects sortUsingDescriptors:[NSArray arrayWithObject:titleSorter]; 
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获得更新的位置。