iOS:lldb EXC_BAD_ACCESS 自定义单元格

标签 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 的消息错误。很确定这是一个内存管理错误。据我所知,我已妥善保留和发布。该应用程序会在启动时崩溃,有时它会加载一切正常,但当您滚动时它会崩溃。感谢您提供有关内存管理的任何帮助或指示。

最佳答案

取而代之的是:

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

您发送了自动释放消息并将其设置为 nil,稍后您将尝试访问已释放的 cell

我认为应该是:

static NSString *CellIdentifier = @"LibraryListingCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

关于iOS:lldb EXC_BAD_ACCESS 自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15726970/

相关文章:

objective-c - objective-c :按下按钮后切换属性的BOOLEAN值的问题

ios - 有没有办法在组件加载之后但显示之前设置单元格的高度?

java - 以编程方式计算 Java 对象占用的内存,包括它引用的对象

ios - 如何防止我不知道的对象上的 EXC_BAD_ACCESS 在 iOS 9 中发布?

ios - 访问 String 属性时的 EXC_BAD_ACCESS

ios - 如何在每个显示文本的触摸按钮后打印空格

ios - 如何在按下 ios 上的 UINavigationBar 后退按钮后隐藏键盘

wolfram-mathematica - Apply与Map的内存使用情况。虚拟内存的使用和锁定

android - Android 中使用的确切内存模型是什么?

core-data - 尝试使用 NSFetchedResultsController 创建 USE_BLOCK_IN_FRAME ... EXC_BAD_ACCESS