MKMapView在UITableViewCell重新加载后变为白色



我有一个MKMapView嵌入到UITableViewCell中。有时,该部分会重新加载。问题是地图单元格在刷新发生时突然决定变为白色。

这是基本代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    _mapCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Map"];
    [_mapCell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [_mapCell setBackgroundColor:[UIColor greenColor]];
    _mapView = [[MKMapView alloc] init];
    [_mapView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_mapCell.contentView addSubview:_mapView];
    [_mapCell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_mapView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_mapView)]];
    [_mapCell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mapView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_mapView)]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_mapCell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return _mapCell;
}

和重新加载代码

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
    });
}

下面是演示该问题的示例项目。


注意:如果与地图一起使用,则表视图的 JIT 会滞后。因此,我为它创建了一个 iVar 并更早地初始化它。

来自文档

reloadSections:withRowAnimation:
Reloads the specified sections using a given animation effect.
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
Parameters
sections  An index set identifying the sections to reload.
animation A constant that indicates how the reloading is to be animated, for 
The animation constant affects the direction in which both the old and the new section rows slide. For example, if the animation constant is UITableViewRowAnimationRight, the old rows slide out to the right and the new cells slide in from the right.
Discussion
Calling this method causes the table view to ask its data source for new cells for the specified sections. The table view animates the insertion of new cells in as it animates the old cells out. Call this method if you want to alert the user that the values of the designated sections are changing. If, however, you just want to change values in cells of the specified sections without alerting the user, you can get those cells and directly set their new values.

具体说来

动画常量影响两个旧 和新部分行滑动。例如,如果动画常量 是 UITableViewRowAnimationRight,旧行向右滑出 新单元格从右侧滑入。

您返回的单元格与表视图也尝试通过动画移开的单元格相同。这就是地图视图消失的原因。

您必须使用此部分来解决

但是,如果您只想更改指定单元格中的值 部分无需提醒用户,您可以获取这些单元格和 直接设置其新值。

使用UITableViewRowAnimationNone也有效

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

这真的很奇怪,因为你的代码看起来不错。我能够重新加载该部分并使用以下代码显示单元格:

[CATransaction begin]; 
[CATransaction setCompletionBlock:^
{ 
     [self.tableView reloadData];
}];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]    
              withRowAnimation:UITableViewRowAnimationAutomatic];
[CATransaction commit];

但这与其说是修复,不如说是一种解决方法。

最新更新