【发布时间】:2015-01-09 17:30:46
【问题描述】:
一旦找到用户的位置并进行反向地理编码,我就会尝试更新 UITableViewCell。
通过阅读许多其他类似问题的答案,似乎 tableview 重新加载必须发生在主线程上,我尝试过但没有成功。
所有位置数据都被正确检索,并正确添加到核心数据对象中,但 tableview 单元格在用户滚动或选择单元格之前不会更新,此时单元格从该点正确更新在。
这是我的代码中的一个选择 - 有人知道为什么 tableview 单元格没有立即更新吗?
- (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];
}
再一次,底层逻辑正在工作,因为数据被正确添加并最终显示在单元格上,但直到用户滚动或选择。我无法想象为什么这不会立即刷新 tableviewcell。有人知道吗?
更新:我的解决方案
缺少的部分是在单元格创建/出列之后添加[cell layoutSubviews]。 detailTextLabel 现在可以正确更新。显然,这可能与一个 iOS8 错误有关,如果它开始为 nil(即没有内容),则不会更新 detailText,并且 layoutSubviews 通过初始化单元格的所有子视图(就我而言)使它不是 nil理解)。我从这里得到了这个建议:
ios 8 UITableViewCell detail text not correctly updating
接受的答案也帮助我找出了这个缺失的部分。
【问题讨论】:
标签: ios objective-c iphone uitableview