MKMapView在UITableViewCell中

5

我有一个包含MKMapView的UITableView和UITableViewCells。

问题是:如果选择表格单元并移动地图,则会看到mapview全部变为白色,只能看到“Legal”标签。

有人之前遇到过类似情况吗?

screenshot

以下是完整代码:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.table.backgroundColor = [UIColor clearColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 220, 70)];

    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
    CLLocationCoordinate2D logCord = CLLocationCoordinate2DMake(47.606, -122.332);
    MKCoordinateRegion region = MKCoordinateRegionMake(logCord, span);
    [map setRegion:region animated:NO];

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aaaa"];
    [cell.contentView addSubview:map];

    return cell;
}
2个回答

4

我有这方面的经验。

在我的情况下,如果地图的tableView或cell具有背景颜色,并且其cell具有灰色或蓝色选择样式,并且该cell通过dequeue被回收,则地图会变成白色。

我认为,如果:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

1
我已经尝试过了,可以确认那并不能解决问题。 - vgr
2
啊,好的,实际上导致我的情况出现白色地图的原因是在viewWillAppear中设置了self.tableView.backgroundColor。在viewDidLoad中设置tableView的backgroundColor不会产生问题。单元格的选择样式并不重要。 - Eric
如果有帮助的话,如果你在使用nib来创建UITableViewCell,则可能该nib的selectionStyle被设置为与程序中设置的值不同。当调用cellForRowAtIndexPath时(其中cell.selectionStyle是以编程方式设置的),nib可能尚未加载,然后选择样式会重置为nib中的值。 - Duncan

0

我也遇到过这个问题。

当我推出或弹出另一个详细视图控制器后,我的地图会变成白色背景。TableView 还没有配置颜色/背景,一切都是默认的。

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

...实际上有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接