Ios 按计算距离排列UITableView单元格
我对iOS开发非常陌生。我遇到了一个问题 我想在我的自定义tableview上按距离排序我的Ios 按计算距离排列UITableView单元格,ios,objective-c,uitableview,parse-platform,Ios,Objective C,Uitableview,Parse Platform,我对iOS开发非常陌生。我遇到了一个问题 我想在我的自定义tableview上按距离排序我的UITableView。 我通过parse.com数据库上的查询获得日期。 然后,在创建单元时,我计算当前位置和距数据库对象的地质点位置之间的距离。-->那很好 但是如何按计算出的距离对TableView进行降序/升序 这是我的代码,我在其中创建单元格并计算距离: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtInd
UITableView
。
我通过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;
}
[query whereKey:@"location" nearGeoPoint:userGeoPoint];
在
cellforrowatinexpath
中,对行进行排序为时已晚,在
单元格即将显示
您应该在获取元素后对其进行排序,并将排序后的数组用作表视图数据源。将模型数据(MVC)与视图更新(CellForRowatineXpath中的代码)分开。i、 e.不要从单元格中调用地理点来查找Background中的CurrentLocation
。执行一次,然后获取所有位置数据
现在,您应该有一个位置数据数组。在cellforrowatinexpath
中,可以使用indepath
从该数组中获取数据
如果要更改结果的顺序,请反转数组。(通常使用反向对象枚举器来完成)。谢谢!
我将MVC和视图更新分开。
然后,我更改了初始查询,并使用以下函数按距离对结果进行排序:
- (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;
}
[query whereKey:@"location" nearGeoPoint:userGeoPoint];
: