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.
You should get a reference to the cell that you want to update
Ex. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:theRow inSection:theSection]];
Then [[cell textLabel] setText:theLocation]
;
If your updating multiple cells just get multiple references of the cells you want to update and update them accordingly.
Typically anything dealing with UI components that need updating should be done on the main thread. I have come across this issue before but if you want to have a solution dealing with threading, what you have done seems like it should work.
Here is the GCD solution:
dispatch_async(dispatch_get_main_queue(), ^{
[self.table reloadData];
});
But according to what you've done, it shouldn't help... It is basically the same thing just with GCD. Hopefully the solution I gave you at the beginning works...
EDIT
Within your - (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
Did you set the style to UITableViewCellStyleDefault
?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments