Reputation: 1439
I'm using the following code to determine the distance between my logged in user and all other users in the database. I'm successfully able to add the found distances to NSMutableArray finalDistances, however I'm now trying to display those distances in my tableview cells in label kmAway. I want to display them in the same order they're returned in each cell.
That said, when I try to implement that as shown in ViewController.m, self.finalDistances is returned as null in my tableview, even though it's populated inside the viewDidLoad. How can I accomplish this?
ViewController.h
@property (nonatomic) CLLocationDistance kilometers;
@property (nonatomic) CLLocation *startLocation;
@property (nonatomic) CLLocation *endLocation;
@property (strong, nonatomic) NSMutableArray *finalDistances;
ViewController.m
NSString *myLocation = self.currentUser[0][@"street_address"];
NSLog(@"MY LOCATION %@", myLocation);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:myLocation completionHandler:^(NSArray* placemarks2, NSError* error) {
for (CLPlacemark* aPlacemark in placemarks2)
{
for (NSMutableDictionary *multiplelocationsFriend in self.closeByNeighbours) {
NSString *location = [multiplelocationsFriend objectForKey:@"address"];
CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
[geocoderFriend geocodeAddressString:location completionHandler:^(NSArray* placemarks, NSError* error) {
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
self.endLocation = [[CLLocation alloc] initWithLatitude:placemark.location.coordinate.latitude longitude:placemark.location.coordinate.longitude];
NSString *myLocation = self.currentUser[0][@"street_address"];
NSLog(@"MY LOCATION %@", myLocation);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:myLocation completionHandler:^(NSArray* placemarks2, NSError* error) {
for (CLPlacemark* aPlacemark in placemarks2)
{
self.startLocation = [[CLLocation alloc] initWithLatitude:aPlacemark.location.coordinate.latitude longitude:aPlacemark.location.coordinate.longitude] ;
self.kilometers = [self.startLocation distanceFromLocation:self.endLocation] / 1000;
self.finalDistances = [NSMutableArray new];
[self.finalDistances addObject:[NSNumber numberWithDouble:self.kilometers]];
[self.neighboursView reloadData];
}
}];
}
}];
}
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
[self.neighboursView reloadData];
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", [error localizedDescription]);
}];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NetworkTableIdentifier = @"sidebarCell";
self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (tableView == self.neighboursView)
{
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
NSDictionary *userName = [self.closeByNeighbours objectAtIndex:indexPath.row];
NSString *first = [userName objectForKey:@"first name"];
NSString *last = [userName objectForKey:@"last name"];
NSString *area = [userName objectForKey:@"neighbourhood"];
NSString *city = [userName objectForKey:@"city"];
NSDictionary *distances = [self.finalDistances objectAtIndex:indexPath.row];
[[cell kmAway] setText:[NSString stringWithFormat:@"%@", distances]];
[[cell areaLabel] setText:[NSString stringWithFormat:@"%@, %@", area, city]];
NSDictionary *userBio = [self.closeByNeighbours objectAtIndex:indexPath.row];
[[cell username] setText:[NSString stringWithFormat:@"%@ %@", first, last]];
NSString *profilePath = [[self.closeByNeighbours objectAtIndex:indexPath.row] objectForKey:@"photo_path"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
return cell;
}
...
Upvotes: 0
Views: 45
Reputation: 10811
self.finalDistances
isn't set until the completion handler on CLGeocoder
is run. Additionally, it is reset to an empty NSMutableArray
each pass through the for
loop in the completion handler.
However, there may be other bugs as well.
Upvotes: 1