私はObjective-Cについて初心者です。専門家が私を助けてくれることを願っています。 JSONエンコーディングで名前と住所を返すWebサービスがあります。私のアプリ側では、サブタイトルスタイルのセルを持つUITableView
があります。私は主ラベルの名前と現在のデバイスの位置からのアドレスの距離を2番目のラベルに表示しています。私は距離上でUITableViewCell
を並べ替える必要があります。私は名前と距離を表示することができますが、距離をソートする方法はわかりません。助けていただければ幸いです。UITableViewCellソート
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *info = [json objectAtIndex:indexPath.row];
NSString *sName = [info objectForKey:@"Name"];
NSString *sAddress = [info objectForKey:@"Address"];
cell.textLabel.text = sName;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:sAddress completionHandler:^(NSArray *placemarks, NSError *error) {
if (error){
NSLog(@"%@", error);
} else {
CLPlacemark *placemark = [placemarks lastObject];
CLLocation *restLocation = [[CLLocation alloc] initWithLatitude:placemark.location.coordinate.latitude longitude:placemark.location.coordinate.longitude];
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:self.locationManager.location.coordinate.latitude longitude:self.locationManager.location.coordinate.longitude];
CLLocationDistance distance = [userLocation distanceFromLocation:restLocation];
distance = distance/1000;
NSString *sDistance = [NSString stringWithFormat: @"%@%1.2f%@",@"Distance: ",distance, @" km"];
cell.detailTextLabel.text = sDistance;
}
}];
return cell;
}
実際にセルをソートしません。テーブルビューを作成するために使用するデータをソートする必要があります。テーブルビューを読み込む前にこれを行います。その後、セルはデータと同じ順序で表示されます。 – rmaddy