I am trying to update a UITableViewCell as soon as the user's location is found and reverse-geocoded.
From reading lots of other answers to similar questions, it seems the tableview reload must occur on the main thread, which I have tried without any success.
All the location data gets retrieved correctly, and is correctly added to the core data object, but the tableview cell simply is not updating until the user scrolls or the cell is selected, at which point the cell is correctly updated from that point on.
Here is a selection from my code - does anyone know why the tableview cell isn't updating right away?
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations // Delegate callback method.
{
// Correctly gets currentLocation coordinates.
...
CLLocation *currentLocation = [locations lastObject];
...
// Reverse-geocode the coordinates (find physical address):
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
CLPlacemark *placemark = [placemarks lastObject]; // Correctly gets placemark.
// Correctly adds placemark to core data object.
...
...
// Reload TableView:
// [self.tableView reloadData]; //Tried this, didn't work, since not on main thread.
// [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; //Doesn't work.
// [self performSelector:(@selector(refreshDisplay)) withObject:nil afterDelay:0.5]; //Doesn't work.
[self performSelectorOnMainThread:@selector(refreshDisplay) withObject:nil waitUntilDone:NO]; //Doesn't work.
}
}];
}
- (void)refreshDisplay {
[_tableView reloadData];
}
Again, the underlying logic is working, since the data gets added correctly and eventually shows up on the cell, but not until user scroll or selection. I can't imagine why this doesn't refresh the tableviewcell right away. Does anyone have any idea?
UPDATE: My Solution
The missing piece was adding [cell layoutSubviews]
just after the cell is created/dequeued. The detailTextLabel now updates correctly. Apparently this may be related to an iOS8 bug that doesn't update the detailText if it starts out nil (i.e. no content), and layoutSubviews makes it so it is not nil, by initializing all the subviews of the cell (as far as I understand). I got this suggestion from here:
ios 8 UITableViewCell detail text not correctly updating
The accepted answer also helped me figure out this missing piece.