【问题标题】:UITableCell subclass never released?UITableCell 子类从未发布?
【发布时间】:2013-08-19 08:24:35
【问题描述】:

我正在使用 ARC,但我的自定义 UITableCellView 似乎没有发布。

TBMListingLineView 是 TBMGlobalCustomCell 的子类,TBMGlobalCustomCell 是 UITableCellView 的子类。

在 TBMListingLineView 中有 10 个 UILabel(非原子,保留)

我在两个类中都实现了从不调用的方法 dealloc(断点不会停止执行)

当我滚动 TableView 时,UILabel 的数量在 Instruments/Allocations 中增加,这导致应用程序在多次内存警告后崩溃。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

static NSString *CellIdentifier = @"Cell";

TBMGlobalCustomCell* cell;

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

switch(sortIndex) {
    case 0 :
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil || ![cell isKindOfClass:[TBMListingLineView class]]) {
            cell = [[TBMListingLineView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

    break;

....

return cell;

}

【问题讨论】:

  • 为什么要给dequeueReusableCellWithIdentifier打两次电话?如果您的自定义单元格是TBMGlobalCustomCell 对象,为什么还要检查[TBMListingLineView class]
  • 为什么要调用两次dequeueReusableCellWithIdentifier? -> 天哪……就是这样!谢谢 !为了回答你的第二个问题,我有 5 个不同的 TBMGlobalCustomCell 子类,并且根据 sortIndex 值,TableCellView 是不同的,再次感谢
  • 但是调用 dequeueReusableCellWithIdentifier 然后用新分配的单元格替换出队的单元格也没有意义。更好地为每个子类使用不同的单元格标识符。
  • 是的,你是对的,我刚刚检查了文档,我不知道这些标识符的目的

标签: ios memory-leaks automatic-ref-counting instruments


【解决方案1】:

第一个问题是您为每个单元格调用了两次dequeueReusableCellWithIdentifier。 然后,如果第二个出列单元格没有正确的类,你也会“扔掉”它。

更好的解决方案是为每个单元格(子)类使用不同的单元格标识符 表视图,以便dequeueReusableCellWithIdentifier 返回正确的实例 类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    相关资源
    最近更新 更多