当我将 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”的单元格标识符。
这是我的
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,错误就会消失。
关于我做错了什么有任何线索吗?
问题是你给了“CELL”并且没有“Cell”,如果你在IB或恢复ID中设置标识符,你还需要仔细检查,你必须设置的标识符在“属性检查器”中
好吧,问题是我在属性检查器中选择了动态单元格,但 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;
}
我也偶然发现了这一点...我的错误,注意:'register(nib' 调用,其中 'nib' 不是刚刚加载的 nib 对象 .......... 错误代码
让 myNib = UINib(nibName:"MyCellNib", 捆绑包: nil);
numberMapCollectionView.register(nib, forCellWithReuseIdentifier: "MyCellNib");
numberMapCollectionView.dataSource = self;
...好的代码 注意:注册(myNib
让 myNib = UINib(nibName:"MyCellNib", 捆绑包: nil);
numberMapCollectionView.register(myNib, forCellWithReuseIdentifier: "MyCellNib");
numberMapCollectionView.dataSource = self;
我使用情节提要也遇到了类似的错误。通过将表视图的“原型单元格”@“属性检查器”更改为 1 来修复此问题。