samedi 3 janvier 2015

Order UITableView cells by calculated distance (Parse)

I have a problem with the database created on parse. I have a table that shows me how many miles of the various local (once you download the list from DB) the problem is that I would order it for distance upward, but I do not know how to do. The code I used to calculate the distance is this:



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = @"RecipeCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

NSString *AntDef = [NSString stringWithFormat:@"%@", [object objectForKey:@"AnteprimaDefault"]];

if ([AntDef isEqualToString:@"Null"])
{
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.image = [UIImage imageNamed:@"ImageDefault.png"];
thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.width / 2;
thumbnailImageView.clipsToBounds = YES;
thumbnailImageView.layer.borderWidth = 0.5;
thumbnailImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}

else
{
PFFile *thumbnail = [object objectForKey:@"Anteprima1"];
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.image = [UIImage imageNamed:@"placeholder.jpg"];
thumbnailImageView.file = thumbnail;
thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.width / 2;
thumbnailImageView.clipsToBounds = YES;
[thumbnailImageView loadInBackground];
}



Lat = [[object objectForKey:@"Latitudine"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Long = [[object objectForKey:@"Longitudine"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Latdouble = [Lat doubleValue];
Longdouble = [Long doubleValue];


NSArray *locations = [NSArray arrayWithObjects:[[CLLocation alloc] initWithLatitude:Latdouble longitude:Longdouble], nil];

//NSLog(@"LOCATIONS COORDINATE: %@", locations);

CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];;
CLLocation *nearestLoc = nil;
CLLocationDistance nearestDis = FLT_MAX;

for (CLLocation *location in locations) {
CLLocationDistance distance = [currentLocation distanceFromLocation:location];

for (CLLocation *location in locations) {
distance = [currentLocation distanceFromLocation:location];
if (nearestDis > distance) {
nearestLoc = location;
nearestDis = distance;
}
}

text12 = [NSString stringWithFormat:@"%.1f Km", nearestDis/1000];
}

...
}


Thanks in advance :)




Aucun commentaire:

Enregistrer un commentaire