防止在UITableViewCell内重新加载MKMapView

时间:2011-11-02 21:09:14

标签: iphone cocoa-touch uitableview mkmapview

我已经创建了UITableViewCell的子类并添加了一个MapView。每次我滚动屏幕的地图并滚动回那个单元格,它再次加载。 (众所周知,这是细胞重用的默认行为)

反正有没有阻止它?或者你知道那种情况的其他技巧吗?非常感谢!

1 个答案:

答案 0 :(得分:4)

一种可能的解决方案:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";
  NSInteger row = [indexPath row];
  UITableViewCell *cell = nil;
  if( row != kMapCellRow ) { // defined value to whatever value it may be
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  }
  else {
    // use an ivar to track the map cell, should be set to nil in class init
    cell = _mapCell;
  }
  if (cell == nil) {
    if( row != kMapCellRow ) { // defined value to whatever value it may be
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    else {
        // create your map cell here
        cell = [[[MyTableViewMapCell alloc] init...
    }
  }
  // configure it here

如果对您的应用有意义,您可以用实例变量替换已定义的kMapCellRow。