【发布时间】:2010-02-09 15:41:33
【问题描述】:
我在我的一个 TableView 中使用工具发现了内存泄漏,正好在这一行:
[[NSBundle mainBundle] loadNibNamed:@"ShopListCell" owner:self options:NULL];
来自 nib ShopListCell 的标识符与 CellIdentifier 不正确。
现在,我没有内存泄漏,但我的 UITableViewCells 有自己的生命 :-)
我正在使用自定义 UITableViewCell,并显示一些图像并从 NSFetchedResultsController 更新一些标签。
当用户点击某一行时,我会更新模型,因此单元格始终会显示真实数据,但它不会显示真实数据,而是显示其他单元格。
我怀疑这是因为我在重复使用单元格,但我在返回之前对单元格进行了所有修改,因此我希望始终显示正确的数据。
在修复内存泄漏之前这是完美的,我一直在使用一个新的单元,现在我正在重复使用它们,但有很多问题。
[cell setNeedsDisplay];在返回单元格之前没有任何效果。
这里有一些代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ShopListCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
[[NSBundle mainBundle] loadNibNamed:@"ShopListCell" owner:self options:NULL];
cell = nibLoadCell;
cell.backgroundColor = [UIColor clearColor];
}
// Set up the cell...
Ingredient *ingredient = (Ingredient *)[fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Section %d Row %d ingredient: %@", indexPath.section, indexPath.row,ingredient.name); // just to be sure it fetchs the correct data, and it does
if([ingredient.isInListDone intValue] == 0) {
cell.accessoryType = UITableViewCellAccessoryNone;
[cellStateButton setSelected:NO];
cellImageInList = nil;
}
else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cellStateButton setSelected:YES];
cellImageInList.image = [UIImage imageNamed:@"underlined2.png"];
}
cellLabelName.text = [ingredient name];
[cell setNeedsDisplay]; // this line has NO effect
return cell;
}
我还放了一个 NSLog,它在正确的部分和行中获取正确的数据...
谢谢,
r.
【问题讨论】:
标签: iphone uitableview