2012-01-11 31 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獲得更新的位置。