2016-08-13 37 views
0

上下文: 我正在使用包含許多不同單元格的TableView:其中一個應該包含一個MapView,其中包含多個批註。iOS 9:在UITableViewCell中帶有多個批註的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 [email protected]"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我t不起作用。 我試圖NSLog的一切,但輸出是相同的,所以mapPinForAddress每次被調用。

有沒有人有一些提示或者一些代碼?

+1

如果調用次數太多,geocodeAddressString會失敗。 – Brandon

回答

1

將mapview放置在uitableviewcell中是一個糟糕的做法,除非您真的需要這樣做。最好的方法是爲位置創建靜態圖像,並使用uiimageview而不是mapview來使用它。這裏是從地圖創建靜態圖像的API - >Click Here