iOS: lldb EXC_BAD_ACCESS Custom Cell

戏子无情 提交于 2019-12-12 06:31:14

问题


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

Message ERROR for cell.name.text cell.week.text cell.image.text. Pretty sure it is a memory management error. I have retained and released properly to the best of my knowledge. The application will crash upon launch, sometimes it loads everything fine, but when you scroll it crashes. Any help or pointers about memory management is appreciated.


回答1:


Instead of this:

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

You sent autorelease message and set it to nil, later you are trying to access that released cell.

I think it should be as:

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

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


来源:https://stackoverflow.com/questions/15726970/ios-lldb-exc-bad-access-custom-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!