[英]'NSInternalInconsistencyException' reason: 'Could not load NIB in bundle' when dequeuing tableview cell
[英]Could not load NIB in bundle ! Customs Cell in TableView
我的tableView
有问题。 我想在tableView
有6个不同的单元格。
所以,在viewDidLoad
,我有这个:
[_tableView registerNib:[UINib nibWithNibName:@"KBCategoriePriceTableViewCell" bundle:nil]
forCellReuseIdentifier:@"KBCategoriePriceTableViewCell"];
对于其他单元格,它是相同的代码,并且在cellForRowAtIndexPath
:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
NSString* nameCell = [array objectAtIndex:indexPath.row];
if([nameCell isEqualToString:CELL_VIEW_CATEGORY_PRICE]){
KBCategoriePriceTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"KBCategoriePriceTableViewCell"];
[cell setBackgroundColor:[[receive category] colorBackground]];
[cell.buttonCategorie setTitle:@"" forState:UIControlStateNormal];
[cell.buttonCategorie setEnabled:NO];
[cell.buttonCategorie setImage:[UIImage imageNamed:[[receive category] imageName]]forState:UIControlStateNormal];
[cell.buttonDevise setTitle:[[receive devise] symbole] forState:UIControlStateNormal];
[cell.buttonCategorie setEnabled:NO];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
}
但是当我启动我的应用程序时,它崩溃了! 出现此错误消息:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/jean-nicolasdefosse/Library/Application Support/iPhone Simulator/7.1/Applications/20031DF6-D297-44D0-9D67-4AD3439D85F7/KillTheBill.app> (loaded)' with name 'KBCategoriePriceTableViewCell''
我不明白为什么它不起作用。
我删除引用并在项目中添加文件,然后检查目标和自定义单元KBCategoriePriceTableViewCell
作为“ xib”中的标识符: KBCategoriePriceTableViewCell
。
请帮我 !
错误消息表明您的项目中没有名为KBCategoriePriceTableViewCell.nib
的文件。
另外,您应该始终在tableView:cellForRowAtIndexPath:
返回一个单元格。
您需要像下面的代码一样加载自定义单元格。
您还需要检查nil条件,因为最初我们不需要加载任何自定义单元。
static NSString *cellidentifier=@"cell";
KBCategoriePriceTableViewCell *cell=(KBCategoriePriceTableViewCell *)[explortableview dequeueReusableCellWithIdentifier:cellidentifier];
if (cell==nil)
{
[[NSBundle mainBundle]loadNibNamed:@"KBCategoriePriceTableViewCell" owner:self options:nil];
cell=self.categoriesCell; //categoriesCell is reference of KBCategoriePriceTableViewCell in which you connected at xib
}
希望对您有帮助。
不要在viewDidLoad
加载Nib文件。
您可以使用以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=@"KBCategoriePriceTableViewCell";
KBCategoriePriceTableViewCell *cell=(KBCategoriePriceTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(cell==nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"KBCategoriePriceTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lbl_name.text=@"Meeting";
return cell;
}
在这里, cell = [nib objectAtIndex:0];
是第一个可以加载动态单元格的单元格。
注意不要使用initWithXib调用原型单元,因为那样会崩溃
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.