我对iOS开发很陌生。我遇到了一个问题。
我想UITableView
在我的自定义 tableview 上订购我的 Descending by Distance。我通过查询我的 parse.com 数据库获得日期。然后,在创建单元格时,我会计算我当前位置与数据库对象的地理点位置之间的距离。--> 效果很好!
但是如何按计算的距离对 TableView 进行降序/升序排序?
这是我的代码,我在其中创建单元格并计算距离:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = @"stationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell
PFFile *thumbnail = [object objectForKey:@"img"];
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.image = [UIImage imageNamed:@"placeholder.jpg"];
thumbnailImageView.file = thumbnail;
[thumbnailImageView loadInBackground];
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = [object objectForKey:@"name"];
UILabel *adressLabel = (UILabel*) [cell viewWithTag:102];
adressLabel.text = [object objectForKey:@"adress"];
// DISTANCE Calculation
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *currentLocationGeoPoint, NSError *error) { //Get current Location
if (!error) {
PFGeoPoint *distanceGeoPoint = [object objectForKey:@"location"];
double distanceDouble = [currentLocationGeoPoint distanceInKilometersTo:distanceGeoPoint];
NSLog(@"Distance: %.1f",distanceDouble); // %.1f - limits to 1.1
UILabel *distanceLabel = (UILabel*) [cell viewWithTag:103];
distanceLabel.text = [NSString stringWithFormat:@"%.1f", distanceDouble];
}
}];
return cell;
}