gpt4 book ai didi

ios - UITableViewCell 重新加载后 MKMapView 变白

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:30 26 4
gpt4 key购买 nike

我有一个嵌入到 UITableViewCell 中的 MKMapView。有时,该部分会重新加载。问题是当刷新发生时, map 单元决定突然变白。

这是基本代码

- (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];
});
}

Here is the sample project to demonstrate the problem.


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

最佳答案

来自docs

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.

特别是

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.

您正在返回表格 View 也试图通过动画移开的同一个单元格。这就是 map View 消失的原因。

你将不得不使用这部分来解决

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.

使用 UITableViewRowAnimationNone 也有效

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

关于ios - UITableViewCell 重新加载后 MKMapView 变白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22024178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com