【问题标题】:iOS: lldb EXC_BAD_ACCESS Custom CelliOS:lldb EXC_BAD_ACCESS 自定义单元
【发布时间】:2013-03-31 06:23:23
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"LibraryListingCell";

    InSeasonCell *cell = (InSeasonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"InSeasonCellView" owner:self options:nil];
        cell = [_cell autorelease];
        _cell = nil;
    }
    if(_dataController!=NULL){
        Product *productAtIndex = [_dataController objectInListAtIndex:indexPath.row];
        // Configure the cell...
        if (productAtIndex.name != nil && productAtIndex.week != nil && productAtIndex.image != nil) {
            cell.name.text = productAtIndex.name;
            cell.week.text = productAtIndex.week;
            cell.image.image = productAtIndex.image;
        }
    }

    return cell;
}

cell.name.text cell.week.text cell.image.text 的消息错误。很确定这是一个内存管理错误。据我所知,我已妥善保留和释放。应用程序在启动时会崩溃,有时它会加载一切正常,但是当你滚动它时它会崩溃。任何有关内存管理的帮助或指针表示赞赏。

【问题讨论】:

    标签: ios memory-management exc-bad-access lldb


    【解决方案1】:

    而不是这个:

     if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"InSeasonCellView" owner:self options:nil];
        cell = [_cell autorelease];
        _cell = nil;
      }
    

    您发送了 autorelease 消息并将其设置为 nil,稍后您将尝试访问已发布的 cell。

    我觉得应该是这样的:

    static NSString *CellIdentifier = @"LibraryListingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    

    【讨论】:

    • _cell 是一个自定义 UITableViewCell,我将其作为 IBOutlet 访问。我认为我不必发布 IBOutlet 或分配一个。我将尝试使用 IBOutlet 而不释放它。
    • 我正在使用本教程iphonedevcentral.com/…。
    • 有一个自定义单元格 .h .m 和 xib 文件。我将如何加载(自定义 xib)而不是创建新的 UITableViewCell
    • 找到了。它现在在文档中“从 Nib 文件加载自定义表格视图单元格”link
    猜你喜欢
    • 2013-10-21
    • 1970-01-01
    • 2013-02-06
    • 2011-11-05
    • 2011-05-21
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    相关资源
    最近更新 更多