ios - 位置回调后 UITableViewCell 不更新,直到滚动或选择

标签 ios objective-c iphone uitableview

我正在尝试在找到用户位置并进行反向地理编码后立即更新 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 错误有关,如果 detailText 开始时为 nil(即没有内容),它不会更新 detailText,而 layoutSubviews 通过初始化单元格的所有 subview (据我所知)使其不为 nil理解)。我从这里得到了这个建议:

ios 8 UITableViewCell detail text not correctly updating

接受的答案也帮助我找出了这个缺失的部分。

最佳答案

您应该获得对要更新的单元格的引用

例。 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:theRow inSection:theSection]];

然后 [[cell textLabel] setText:theLocation];

如果您更新多个单元格只是获取您要更新的单元格的多个引用并相应地更新它们。

通常,任何处理需要更新的 UI 组件的事情都应该在主线程上完成。我以前遇到过这个问题,但如果你想要一个处理线程的解决方案,你所做的似乎应该可行。

这是 GCD 解决方案:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.table reloadData];
});

但是根据你所做的,它应该没有帮助......它基本上与 GCD 是一样的。希望我一开始给你的解决方案有效......

编辑

在你的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath

    cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

您是否将样式设置为 UITableViewCellStyleDefault

关于ios - 位置回调后 UITableViewCell 不更新,直到滚动或选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27866009/

相关文章:

iphone - 不时显示完整图像 - iphone

iphone - UITableView 中的 Obj-C iPhone UISegmentedControl

ios - 删除日期输入上的 'clear' 按钮(iOS safari)

java - objective-C : extending UIViewController leads to unexpected sense of "self" and "super"

ios - 如何将对象从一个 View 发送到另一个 View ?

iOS/OSX 相当于 SetEvent() 和 WaitForSingleObject()?

C:当 A >1 时,语句 'return A || 1' 会返回什么?

ios - UITableViewCell 的detailTextLabel 中的文本左对齐和右对齐

iphone - 如何在 iPhone 中创建登录屏幕(作为 UIAlertView)?

iphone - 从文档文件到 NSData