无法在bundle中加载NIB !TableView中的custom Cell



我的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调用原型单元格,因为这会导致崩溃