iOS:lldb EXC_BAD_ACCESS自定义单元格

时间:2013-03-31 06:23:24

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

- (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 的消息错误。很确定这是一个内存管理错误。据我所知,我保留并妥善发布。应用程序将在启动时崩溃,有时它会加载一切正常,但滚动时会崩溃。任何关于内存管理的帮助或指示都表示赞赏。

1 个答案:

答案 0 :(得分:3)

而不是:

 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];
}