关于ios:UITableViewCell不会在位置回调后更新,直到滚动或选择

UITableViewCell not updating after location callback until scroll or selection

我正在尝试在找到用户的位置并对其进行反向地理编码后立即更新UITableViewCell。

从阅读许多其他类似问题的答案之后,看来tableview重装必须在主线程上进行,我尝试过但没有成功。

所有位置数据均已正确检索,并已正确添加到核心数据对象中,但是直到用户滚动或选择了该单元格后,tableview单元格才开始更新,此时该位置的单元格已从该点正确更新上。

这是我的代码中的一个选择-有人知道为什么表视图单元格没有立即更新吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (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,而layoutSubviews通过初始化单元格的所有子视图来使其不为零(据我所知)理解)。我从这里得到了这个建议:

ios 8 UITableViewCell详细文本未正确更新

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


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

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

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

如果您要更新多个单元格,只需获取要更新的单元格的多个引用,然后相应地进行更新。

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

这是GCD解决方案:

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

但是根据您所做的事情,它无济于事……就GCD而言,这基本上是相同的。希望我在开始时给您的解决方案能够起作用...

编辑

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

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

您是否将样式设置为UITableViewCellStyleDefault


我有同样的问题。这里没有答案对我有用。我最终搜索了笔尖的各个方面,发现我的控件(按钮和表格视图)没有连接到outlets,即使在.h文件中选择了圆圈指示器。但是在连接检查器中,它们未连接到outlets。因此必须删除所有.h文件控件,然后重新连接,然后降低并确保一切正常。


似乎表视图单元格在视图中并不完全可见。在这种情况下,您可以使用[cell setNeedsDisplay];例如:dispatch_async(dispatch_get_main_queue(),^ {[cell setNeedsDisplay];

1
2
    [cell.contentView addSubview:yourView];
});

希望这对您有用。