【发布时间】:2016-08-13 23:52:35
【问题描述】:
背景: 我正在开发一个包含许多不同单元格的 TableView:其中一个应该包含一个包含多个注释的 MapView。
到目前为止我得到了什么:
//address comes in this format: NSString = @"myStreet myNumber, myZIP myCity, Germany";
- (void) mapPinForAddress: (NSString *)address {
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:adresse completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPointAnnotation *adressPoint = [[MKPointAnnotation alloc] init];
adressPoint.coordinate = placeMark.coordinate;
adressPoint.title =@"Title";
adressPoint.subtitle = address;
[mapPins addObject:adressPoint];
}
}];
在cellForRowAtIndexPath内部:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == mapViewSection) {
myMapCell *cell = [tableView dequeue...];
[cell.mapView addAnnotations:mapPins];
[cell.mapView showAnnotations:mapPins animated:YES];
return cell;
}
这是我的问题:
当我第一次加载TableViewController...它就像一个魅力...但是如果我回到以前的视图并再次转到TableViewController,它就不起作用了。
我试过 NSLog 一切,但输出是一样的,所以mapPinForAddress 每次都会被调用。
有没有人有一些提示或一些代码?
【问题讨论】:
-
geocodeAddressString如果调用次数过多,可能会失败。
标签: ios objective-c uitableview mapkit mapkitannotation