UITableCell子类从未发布过?

时间:2013-08-16 20:26:53

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

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

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

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

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

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

enter image description here

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

}

1 个答案:

答案 0 :(得分:2)

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

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