为什么在UITableView中自定义单元格会收到错误:还没有收到initWithFrame:或initWithCoder?

时间:2014-09-26 18:21:37

标签: ios objective-c xcode uitableview

我在youtube上关注了一个教程,我非常仔细地按照所有步骤进行了三次。但我收到了这个错误:

2014-09-27 04:02:42.248 JourneyTracker[6667:306214] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Requesting the window of a view 
(<CustomeCellTableViewCell: 0x8362f6f0; baseClass = UITableViewCell; frame = (0 0; 0 0); 
transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; autoresize = W; layer = (null)>) with a nil layer. This view probably hasn't received initWithFrame: or initWithCoder:.'
            *** First throw call stack:
            (
                0   CoreFoundation                      0x00e0edf6 __exceptionPreprocess + 182

我搜索了许多相关的问题,其中大多数问题,他们忘了初始化按钮等。但正如你在下面看到的那样,我有两个标签并初始化它们:

@implementation CustomeCellTableViewCell{

    UILabel *_nameValue;
    UILabel *_weightValue;

}

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self)
    {
        CGRect nameLableRect = CGRectMake(0, 5, 70, 15);
        UILabel *nameLable = [[UILabel alloc] initWithFrame:nameLableRect];
        nameLable.textAlignment = NSTextAlignmentRight;
        nameLable.text = @"name";
        nameLable.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:nameLable];


        CGRect weightLableRect = CGRectMake(0, 26, 70, 15);
        UILabel *weightLable = [[UILabel alloc] initWithFrame:weightLableRect];
        weightLable.textAlignment = NSTextAlignmentRight;
        weightLable.text = @"weight";
        weightLable.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:weightLable];

        //Add content
        CGRect nameValueRectangle = CGRectMake(80, 5, 200, 15);
        _nameValue = [[UILabel alloc] initWithFrame:nameValueRectangle];
        [self.contentView addSubview:_nameValue];


        CGRect weightValueRectangle = CGRectMake(80, 5, 200, 15);
        _weightValue = [[UILabel alloc] initWithFrame:weightValueRectangle];
        [self.contentView addSubview:_weightValue];

    }
    return self;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void) setName:(NSString *)n
{
    if (![n isEqualToString:_name])
    {
        _name = [n copy];
        _nameValue.text = _name;
    }
}

- (void) setWeight:(NSString *)w
{
    if (![w isEqualToString:_weight])
    {
        _weight = [w copy];
        _weightValue.text = _weight;
    }

}

@end

奇怪的是,当我在awakeFromNib以上进行调试时,从未调用过,在教程中我跟随youtube,他的代码中没有“awakeFromNib”!!

我已经逐行调试了这段代码。首先它进入UITableViewController,它进入自定义类(上面),当它返回到UITableViewController时,它崩溃了。我评论过&lt;&lt;&lt;&lt;&gt;&gt;&gt;&gt;在我的代码中崩溃。

这是我的UITableViewController

@implementation ListJourneyController

static NSString *cellIdentifier = @"CellTableIdentifier";


- (void)viewDidLoad {
    [super viewDidLoad];

 //Some implementation to modify array        

    UITableView *tableView = (id)[self.view viewWithTag:1];
    [tableView registerClass:[CustomeCellTableViewCell class] forCellReuseIdentifier:cellIdentifier];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    return numberOfRow;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//<<<<<This point it goes to customize class and when it comes back it crashes>>>>>>
    CustomeCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    Journey *rowData = items[indexPath.row];
    cell.name = @"afshar";
    cell.weight = @"Hila";
    return cell;

}

更新 解决此问题后,现在两个项目都出现在同一个字段中! enter image description here

1 个答案:

答案 0 :(得分:4)

您永远不会在CustomeCellTableViewCell类中调用super实现。这就是你收到错误的原因。将通话添加到super initWithStyle:reuseIdentifier,您就可以开始了。

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Now do your setup code
    }
    return self;
}