2013-09-23 35 views
0

我想在UITableView的最後一個單元格中顯示MKMapView。UITablViewCell上的MKMapView崩潰

我有一個UITableViewCell中的MKMapView當我滾動UITableViewCell它刷新MKMapView。並且它與崩潰錯誤:MKMapView必須在主線程上初始化。

當我滾動我的UITableView的滾動視圖時,應該怎麼做以防止重新加載MKMapView?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.view endEditing:YES]; 
    if (indexPath.row !=[arrExit count]) //If it is not last cell 
    { 
     static NSString *CellIdentifier = @"BTSTicketsCellIdentifier"; 

     BTSComparePricesCell *cell = (BTSComparePricesCell *)[tblComparePrices dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BTSComparePricesCell" owner:self options:nil]; 
      for (id oneObject in nib) if ([oneObject isKindOfClass:[BTSComparePricesCell class]]) 
       cell = (BTSComparePricesCell *)oneObject; 
     } 

      if (!isDragging_msg && !isDecliring_msg) 
       { 
        [dicImages_msg setObject:[UIImage imageNamed:@"rowDefault.png"] forKey:[[arrExit objectAtIndex:indexPath.row] valueForKey:@"Merchant_Logo"]]; 
        [self performSelectorInBackground:@selector(downloadImage_3:) withObject:indexPath]; 
       } 

     return cell; 
    } 
    else{ //If it is a last cell 

     static NSString *CellIdentifier = @"BTSTicketsCell"; 

     BTSMapViewCell *cell = (BTSMapViewCell *)[tblComparePrices dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BTSMapViewCell" owner:self options:nil]; 
      for (id oneObject in nib) if ([oneObject isKindOfClass:[BTSMapViewCell class]]) 
       cell = (BTSMapViewCell *)oneObject; 
     } 

     CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
     [geocoder geocodeAddressString:[flux objectForKey:@"Venue"] 
        completionHandler:^(NSArray* placemarks, NSError* error) 
     { 
      if (placemarks && placemarks.count > 0) 
      { 
       CLPlacemark *topResult = [placemarks objectAtIndex:0]; 
       MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult]; 

       [cell.MapVw addAnnotation:placemark]; 

       CLLocationCoordinate2D _venue = placemark.coordinate; 

       [cell.MapVw setCenterCoordinate:_venue]; 

       MKCoordinateRegion region = cell.MapVw.region; 
       region.span.longitudeDelta = 1.0; 
       region.span.latitudeDelta = 1.0; 
       [cell.MapVw setRegion:region animated:YES]; 
      } 
     } 
     ]; 
     return cell; 
    } 

} 

在此先感謝。

+0

MKMapView只能在主線程中加載。你在改變線程嗎? – karthika

+0

不,我只寫這麼多的我在上面 – Krunal

+0

寫代碼嘗試此,dispatch_async(dispatch_get_main_queue(),^ { //地圖代碼 }); – karthika

回答

1

試試這個,希望它會工作 在你的視圖控制器很強的參考,您加載表視圖中的地圖創建的MKMapView實例。將經緯度長度值分配給該地圖視圖實例。在CellforRowAtIndexpath上設置cell.MapVw = mapViewInstance。

+0

這裏只是崩潰NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@「BTSMapViewCell」owner:self options:nil];' – Krunal

+0

您確定BTSMapViewCell是有效的nibname。如果你有一個全局的mapview實例,那麼當你滾動和查看tableview時,它不會重新加載 – iGo

+0

是'BTSMapViewCell.xib' – Krunal