我创建了 UITableViewCell 的子类并添加了 MapView。每次我滚动屏幕 map 并滚动回该单元格时,它都会再次加载。 (众所周知,这是单元重用的默认行为)
有什么办法可以防止这种情况发生吗?或者你还知道针对这种情况的其他技巧吗?非常感谢!
最佳答案
一种可能的解决方案:
- (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 替换为实例变量。
关于iphone - 防止在 UITableViewCell 内重新加载 MKMapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7987346/