【问题标题】:dequeueReusableCellWithIdentifier after resolving memoryleaks解决内存泄漏后的 dequeueReusableCellWithIdentifier
【发布时间】: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


    【解决方案1】:

    你正在创建一个单元格

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    

    然后将单元格变量分配给其他东西

    cell = nibLoadCell;
    

    第一行基本上没有效果。我猜想从笔尖加载的单元格仍然没有正确设置其 cellIdentifier。看这里: Loading a Reusable UITableViewCell from a Nib

    【讨论】:

      【解决方案2】:

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

      谢谢,

      r.

      【讨论】:

      • 还有什么帖子?如果你的意思是另一个答案,你为什么不接受它而不是你自己的?
      猜你喜欢
      • 2013-09-02
      • 2015-06-28
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2011-08-14
      相关资源
      最近更新 更多