UITableViewCell中的MKMapView

时间:2012-12-04 11:07:53

标签: objective-c ios uitableview mkmapview

我在MKMapView中有UITableViewCell且禁用了用户互动。当我滚动UITableViewCell时,它会刷新所有MKMapViews

我已经在这里阅读了其他答案,其中一个说不使用dequeueReusableCellWithIdentifier(我不是),另一个说dealloc mapView(我是使用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;
}

3 个答案:

答案 0 :(得分: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)动态接受坐标。

答案 1 :(得分:0)

使用@property在头文件中创建一个uitableview单元格全局对象,如果单元格为零,则将单元格指定给全局对象。

然后每次进入cellforrowatindexpath检查对象是否为nil。如果不是nil,则返回全局对象。

//在class.h中

@property (nonatomic, retain) UITableViewCell *mapCell;

//在cellForRowAtIndexPath

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

答案 2 :(得分:-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;