【发布时间】: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