Assertion failure in UITableView configureCellForDisplay:forIndexPath:

前端 未结 6 1869
春和景丽
春和景丽 2020-12-13 17:03

I\'m not sure where the error is here, having looked at other similar issues. I received an Assertion failure.

Terminating app due to uncaught exception \'N         


        
6条回答
  •  悲哀的现实
    2020-12-13 18:00

    You need to call "initWithStyle" in custom TableViewCell and initialise the objects again.

    Example: ProductTableViewCell.m file

    @implementation ProductTableViewCell
    
    - (void)awakeFromNib {
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
       [super setSelected:selected animated:animated];
    }
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
        {
            self.selectionStyle = UITableViewCellSelectionStyleNone;
            _titleLabel = [[UILabel alloc] initWithFrame:(CGRectMake(70, 0, 320, 60))];
            [self.contentView addSubview:_titleLabel];
       }
       return self;
    }
    

    In the main implementation file

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
        ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productTableViewCell"];
        NSDictionary *dic = nil;
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            dic = [_filteredArray objectAtIndex:indexPath.row];
        } else {
            dic = [_originalArray objectAtIndex:indexPath.row];
        }
        cell.titleLabel.text = [dic objectForKey: @"title"];
        return cell;
    }
    

提交回复
热议问题