当我将 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,错误就会消失。
关于我做错了什么的任何线索?
问题是你给"CELL"而没有"Cell"也有一个双重检查,如果你在IB或恢复ID中设置了标识符,你必须设置的标识符在"属性检查器"中
好的,问题是我在属性检查器中选择了动态单元格,但单元格中的代码错误。
- (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;
}
我在使用故事板时遇到了类似的错误。通过将表视图的"原型单元格"@"属性检查器"更改为 1 来修复它。
发现了这个。我的错误是register(nib
调用,其中"nib"不是刚刚加载的nib对象。这是错误的代码:
let myNib = UINib(nibName:"MyCellNib", bundle: nil);
numberMapCollectionView.register(nib,
forCellWithReuseIdentifier: "MyCellNib");
numberMapCollectionView.dataSource = self;
这是好的代码,注意:register(myNib
而不是register(nib
let myNib = UINib(nibName:"MyCellNib", bundle: nil);
numberMapCollectionView.register(myNib,
forCellWithReuseIdentifier: "MyCellNib");
numberMapCollectionView.dataSource = self;