Iphone 解析memoryleaks后,将ReusableCellWithIdentifier退出队列

Iphone 解析memoryleaks后,将ReusableCellWithIdentifier退出队列,iphone,uitableview,Iphone,Uitableview,我在我的一个TableView中发现了一个使用仪器的memoryleak,正好在以下行: [[NSBundle mainBundle] loadNibNamed:@"ShopListCell" owner:self options:NULL]; nib ShopListCell中的标识符与CellIdentifier不正确 现在,我没有内存泄漏,但我的UITableViewCells有自己的生命:-) 我正在使用自定义UITableViewCell,并从NSFetchedResultsCont

我在我的一个TableView中发现了一个使用仪器的memoryleak,正好在以下行:

[[NSBundle mainBundle] loadNibNamed:@"ShopListCell" owner:self options:NULL];
nib ShopListCell中的标识符与CellIdentifier不正确

现在,我没有内存泄漏,但我的UITableViewCells有自己的生命:-)

我正在使用自定义UITableViewCell,并从NSFetchedResultsController显示一些图像和更新一些标签

当用户单击一行时,我会更新模型,因此单元格始终显示真实数据,但是,它显示的不是真实数据,而是其他单元格

我怀疑这是因为我在重用单元格,但我在返回单元格之前对其进行了所有修改,因此我希望始终显示正确的数据

这在修复内存泄漏之前是完美的,我一直在使用一个新的单元,现在我正在重用它们,但是有很多问题

[单元格设置需要显示];在返回之前,单元格没有任何效果

下面是一些代码:

- (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、

您正在使用

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
然后使用

cell = nibLoadCell;
第一行基本上没有效果。我猜从nib加载的单元格仍然没有正确设置其cellIdentifier。看这里:

在阅读了另一篇文章后,我终于用我的自定义单元格设置了一个新类,现在一切正常,没有内存泄漏

谢谢


r、

还有什么帖子?如果你的意思是另一个答案,为什么你不接受它而不是你自己的?