为标识符注册的无效nib(Cell)

时间:2013-11-06 17:54:42

标签: ios objective-c uitableview identifier

当我将segue拖到另一个要推送的视图控制器时,在UITableView中出现以下错误。

"invalid nib registered for identifier (Cell) - nib must contain exactly one top level object which must be a `UITableViewCell` instance"

我的故事板上有UITableView。我通过故事板在UITableView中放入两个静态行。我通过故事板为这些行提供了一个“Cell”的cellIdentifier。

这是我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    [cell.textLabel setFont:[UIFont fontWithName:@"GothamRounded-Light" size:18]];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 13, 300, 30)];
    label.textColor = [UIColor statLightBlue];
    [label setFont:[UIFont fontWithName:@"Rounded-Light" size:18]];
    [cell.contentView addSubview:label];

    if(indexPath.row == 0){
        label.text = @"Credit Card"; // man profile
        cell.imageView.image = [UIImage imageNamed:@"CreditCardIcon.png"];    
    } else if (indexPath.row == 1) {
        label.text = @"Promotion Code"; // credit card
        cell.imageView.image = [UIImage imageNamed:@"PromoCodeIcon.png"];
    }

    return cell;
}

此时,一切都很好。但这就是我所做的导致上述错误的原因:

如果我控制从故事板中的第一个UITableView单元格拖动到创建segue的任何其他随机视图,则会导致上述错误。如果我删除了所说的segue,那么错误就会消失。

关于我做错了什么的任何线索?

3 个答案:

答案 0 :(得分:0)

问题是你提供“CELL”并且如果你在IB或者Restoration ID中设置了标识符,那么“Cell”也没有仔细检查,你必须设置的标识符在“Attributes inspector”中

答案 1 :(得分:0)

好的,问题是我在属性检查器中选择了动态单元格,但是在cellForRowAtIndex中的代码错误。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [super tableView:tableView
                   cellForRowAtIndexPath:indexPath];


[cell.textLabel setFont:[UIFont fontWithName:@"Rounded-Light" size:18]];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 13, 300, 30)];
label.textColor = [UIColor statLightBlue];
[label setFont:[UIFont fontWithName:@"Rounded-Light" size:18]];
[cell.contentView addSubview:label];


if(indexPath.row == 0){
    label.text = @"Credit Card"; // man profile
    cell.imageView.image = [UIImage imageNamed:@"CreditCardIcon.png"];

} else if (indexPath.row == 1) {
    label.text = @"Promotion Code"; // credit card
    cell.imageView.image = [UIImage imageNamed:@"PromoCodeIcon.png"];
}

return cell;

}

答案 2 :(得分:-1)

我使用故事板得到了类似的错误。通过将Table View的“Prototype Cells”@“Attributes inspector”更改为1来修复它。

相关问题