Luc*_*uca 8 objective-c uitableview ios
我正在使用自定义UITableViewCell.当我尝试运行我的代码时,我得到了这个异常堆栈,我无法理解问题的根源:
terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier ((null)) - nib must contain exactly one top level object which must be a UITableViewCell instance'
Run Code Online (Sandbox Code Playgroud)
请注意我没有使用Storyboard.
编辑:
这是我根据断点导致问题的相关代码:
[tableviewSupport registerNib:[UINib nibWithNibName:@"HotelCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CustomCellCId];
Run Code Online (Sandbox Code Playgroud)
Rya*_*ner 30
另一种方案:
我有UITableViewCell,但在我的XIB中有一个额外的对象,一个我错放的标签.
因此,请仔细检查您的XIB,以确保左侧的对象表中只有一个对象.

我不太确定您在提供的代码中想要做什么。您将在 tableView:cellForRowAtIndexPath: 中卸载您的笔尖。您可以使用以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Assuming you set a reuse identifier "cellId" in the nib for your table view cell...
HotelCell *cell = (HotelCell *)[tableView dequeueReusableCellWithIdentifier:@"cellId"];
if (!cell) {
// If you didn't get a valid cell reference back, unload a cell from the nib
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"HotelCell" owner:nil options:nil];
for (id obj in nibArray) {
if ([obj isMemberOfClass:[HotelCell class]]) {
// Assign cell to obj
cell = (HotelCell *)obj;
break;
}
}
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
现在,根据您发布的错误,iOS 期望数组中有一个对象,因此 for 循环可能是不必要的。我通过检查是否 [nibArray count] == 1 然后获取 objectAtIndex:0 并将其分配给单元格而不是循环来完成此操作,这也有效。但是,如果有一天 Apple 决定在 nibArray 中添加其他内容,for 循环可以保护您免受这种情况的影响。
| 归档时间: |
|
| 查看次数: |
10005 次 |
| 最近记录: |