【问题标题】:Odd UITableView height behavior with NSZombieEnabledNSZombieEnabled 的奇怪 UITableView 高度行为
【发布时间】:2011-03-19 15:15:54
【问题描述】:

我正在设置我的身高:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    CGFloat rowHeight = 0;

    if(indexPath.row == [self.items count]){ //more
        rowHeight = 50.0; //same as moreCell 
    }
    else{
        ChartlyCell *cell = (ChartlyCell*)[self tableView:tblView cellForRowAtIndexPath:indexPath];
        rowHeight = cell.totalHeight;
    }

    return rowHeight;   
}

cell.totalHeight 的计算方法如下:

-(float)totalHeight {
    float h = messageLabel.totalheight + 35;
    if(h < 68) h = 68;
    return h;
}

NSZombieEnabled = NO 时,我的模拟器在没有调试错误的情况下崩溃。 NSZombieEnabled = YES 时模拟器运行良好。不知道如何解决?

更新: 这就是我构建初始化单元格的方式:

cell = [[[ChartlyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier andDelegate:self andChartlyDelegate:self andChartlyObj:myChartlyObject]autorelease];

如果我删除自动释放,一切运行正常。我还是不明白为什么?

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview memory-management


    【解决方案1】:

    请参阅Cocoa Memory Management Guide

    假设cell是一个实例变量,考虑:

    cell = [[[ChartlyCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:CellIdentifier
                                   andDelegate:self
                            andChartlyDelegate:self
                                 andChartlyObj:myChartlyObject]autorelease];
    

    这将导致cell 在池耗尽时被回收,从而使cell 指向一个现在已释放的ChartlyCell 实例。如果你想要一个物体留下来,它必须被保留。 alloc 隐含了保留,autorelease 有效地撤消了保留。删除 autorelease 会保留对象。

    模拟器应该在启用僵尸的情况下返回错误。奇怪的是它没有。通过http://bugreport.apple.com/ 提交错误并附上应用程序崩溃版本的构建副本以及指向此 SO 问题的链接。

    【讨论】: