【问题标题】:Programmatic custom cell memory leak issue程序化自定义单元内存泄漏问题
【发布时间】:2013-06-13 10:59:16
【问题描述】:

我最近切换到“不使用 XIB 进行编程”,但遇到了自定义 TableView 单元格的问题。以前在使用XIB时,我使用了以下代码,效果很好,

NSString *CellIdentifier = @"Cell";
AttachmentCustomCell *cell=(AttachmentCustomCell*)[self.attachmenttableview dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
    NSArray* objects= [[NSBundle mainBundle] loadNibNamed:@"AttachmentCustomCell" owner:nil options:nil];
    AttachmentCustomCell *acell = [objects objectAtIndex:0];
    cell = acell;
}

但现在我正在使用以下内容,这给了我内存泄漏,

    static NSString *CellIdentifier = @"Cell";
ConditionReportCustomCell *cell = (ConditionReportCustomCell*)[self.inventoryTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
    cell = [[ConditionReportCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
return cell;

请告诉我我做错了什么。我知道我不能使用自动释放,因为它会导致应用程序崩溃。

【问题讨论】:

  • 在 Pro 中使用 ARC 吗?
  • 对不起,我忘了提。我没有使用 ARC

标签: ios memory-leaks xcode4.5 autorelease


【解决方案1】:

如果您不使用 ARC,只需添加一个自动释放:

cell = [[[ConditionReportCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease] ;

【讨论】:

  • 我不想使用自动释放。使用 autorelease 会使我的应用程序崩溃。还有其他解决方案吗?
  • @shantanu...它崩溃了?那么你在某个地方还有其他问题,正确的方法是使用自动释放,这里...你是在其他地方释放单元格吗?
  • "任何其他解决方案?"...您的代码泄漏,唯一的方法是释放,如果它崩溃,这意味着您可能正在释放您的代码的其他部分...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
相关资源
最近更新 更多