2012-12-04 35 views
1

我有一個MKMapViewUITableViewCell禁用用戶交互。當我滾動UITableViewCell它刷新所有MKMapViews在UITableViewCell中的MKMapView

我讀過這裏的其他答案,一個說不要使用dequeueReusableCellWithIdentifier(我不是),另一個說deallocmapView(我正在使用ARC)。我不想使用圖像。

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

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

    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ANFeedCell"]; 
    ANFeedTableViewCell *cell = nil; 
    if (cell == nil) { 
     // Load the top-level objects from the custom cell XIB. 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ANFeedTableViewCell" owner:self options:nil]; 
     // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
     cell = [topLevelObjects objectAtIndex:0]; 

     MKMapView *mapView = (MKMapView*)[cell viewWithTag:2]; 

     //Takes a center point and a span in miles (converted from meters using above method)   
     CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(37.766997, -122.422032); 
     MKCoordinateRegion adjustedRegion = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, MilesToMeters(0.5f), MilesToMeters(0.5f))]; 
     [mapView setRegion:adjustedRegion animated:YES]; 


    } 
return cell; 
} 
+1

安置自己的UITableViewCell繪製代碼,這樣我們就可以看到你在做 – Lefteris

+0

添加了相關的代碼,我的問題是什麼。 – arooo

回答

4

問題是要初始化新的細胞,每表格單元格。 因此,您的MapView每次都會重置。

這也會導致內存問題,因爲一些向下滾動的桌面會消耗手機的內存。

堅持可重複使用的單元格,但每次爲單元格配置mapview區域。

這裏是一個很好的例子,讓你開始:

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

    static NSString *CustomCellIdentifier = @"MyCustomTableCell"; 
    ANFeedTableViewCell *cell = (ANFeedTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 

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



    MKMapView *mapView = (MKMapView*)[cell viewWithTag:2]; 

    //Takes a center point and a span in miles (converted from meters using above method) 
    CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(37.766997, -122.422032); 
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, MilesToMeters(0.5f), MilesToMeters(0.5f))]; 
    [mapView setRegion:adjustedRegion animated:YES]; 

    return cell; 
} 

什麼是這個代碼的不同,是的MapView是每個顯示單元的時間配置,不僅當它初始化。 當然,你應該改變代碼以從數據源(即NSArray)動態接受座標。

+0

完美的,一直在我的頭上撓了幾天。 – arooo

-1

試試這個

NSArray *views = [cell.contentView subviews]; 
    for(int i = 0; i < [views count]; i++) 
    { 
     UIView *tempView = [views objectAtIndex:i]; 
     [tempView removeFromSuperview]; 
     tempView = nil; 
    } 

    views = nil; 
+0

這只是刪除了MKMapView。 – arooo

+0

需要的是在定製的UITableViewCell dealloc方法中釋放MKMapView。 –

+0

我試着將我的mapView設置爲零,它似乎不影響它是否重新加載滾動。 – arooo

0

只需使用@property在頭文件中創建一個uitableview單元格全局對象,並將該單元格指定給全局對象(如果單元格爲零)。

然後每次輸入cellforrowatindexpath檢查對象是否爲零。如果不是零,則返回全局對象。

//在class.h

@property (nonatomic, retain) UITableViewCell *mapCell; 

//中的cellForRowAtIndexPath

if (mapCell == nil) { 
         allocate the cell 
        } 
        else{ 
         return mapCell; 
        }