2016-05-05 13 views
0

Ich bin ein Anfänger, wenn es um Objective-C geht und ich hoffe, dass Ihnen Experten helfen können. Ich habe einen Webdienst, der Name und Adresse mit JSON-Codierung zurückgibt. Auf meiner App Seite habe ich eine UITableView mit Untertitel Stil Zellen. Ich zeige Name für das Hauptetikett und Entfernung der Adresse vom aktuellen Gerätestandort im zweiten Etikett. Ich muss UITableViewCell auf Distanz sortieren. Ich kann Name und Entfernung anzeigen, weiß aber nicht, wie ich nach Entfernung sortieren soll. Ich werde jede Hilfe zu schätzen wissen.UITableViewCell Sortierung

- (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; 
} 
+3

Sie sortieren die Zellen nicht tatsächlich. Sie müssen die Daten sortieren, die zum Auffüllen der Tabellenansicht verwendet werden. Tun Sie dies vor dem Laden der Tabellenansicht. Dann erscheinen die Zellen in der gleichen Reihenfolge wie die Daten. – rmaddy

Antwort

0

Dank rmaddy und steven.

Ich konnte GeoCodeFromString nicht arbeiten, also habe ich Google API verwendet, um Koordinaten für die Adressen zu erhalten. Ich modifiziere mein JSON-Array von Adressen, um einen Entfernungsschlüssel hinzuzufügen, und sortiere dann dieses neue Array mit NSSortDescriptor.

1

sortieren Datenquelle anstelle der Zelle, ein wenig vor cellForRowAtIndexPath Delegierten ändern

json = [json sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { 
    NSString *address1 = [obj1 objectForKey:@"Address"]; 
    NSString *address2 = [obj2 objectForKey:@"Address"]; 
    return [self distanceFromAddress:address1] > [self distanceFromAddress:address2]; 
}]; 

- (CLLocationDistance)distanceFromAddress: (NSString *)sAddress { 

//Put your Geocoder method here 

}