Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder UITableView cells by calculated distance
    primarykey
    data
    text
    <p>I'm quite new to iOS development. And I got stuck with a problem.</p> <p>I want to order my <code>UITableView</code> Descending by Distance on my custom tableview. I get the date by a query on my parse.com databse. Then, when creating the cell, I calculate the distance between my current location and the geopoint location from the database object. --> That works fine!</p> <p>But how do I order the TableView descending/ascending by the calculated distance?</p> <p>Here is my code, where I create the cell and calculate the distance:</p> <pre><code>- (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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload