JND
JND

Reputation: 170

Could not load NIB in bundle ! Customs Cell in TableView

I have a problem with my tableView. I want have 6 different cell in the tableView.

So, in the viewDidLoad, i have this:

[_tableView registerNib:[UINib nibWithNibName:@"KBCategoriePriceTableViewCell" bundle:nil]
 forCellReuseIdentifier:@"KBCategoriePriceTableViewCell"];

For other cells, it's the same code and, in the 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;
}
}

But when i launch my app, it's crash ! With this error message :

*** 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''

I don't understand why it doesn't work.

I remove reference and add the files in the project, i check the target and the custom cell KBCategoriePriceTableViewCell have as identifier in "xib": KBCategoriePriceTableViewCell.

Please help me !

Upvotes: 1

Views: 5594

Answers (4)

Ahmed Elashker
Ahmed Elashker

Reputation: 1000

Careful not to invoke a prototype cell using initWithXib because that will crash

Upvotes: 0

sundar
sundar

Reputation: 186

Don't load the Nib file in viewDidLoad.

You can use the code below:

- (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;

}

Here, the cell = [nib objectAtIndex:0]; is the first cell like that you can load the dynamic cells.

Upvotes: 0

rckoenes
rckoenes

Reputation: 69499

Error message is saying that you do not have a file named KBCategoriePriceTableViewCell.nib in your project.

Also you should always return a cell in the tableView:cellForRowAtIndexPath:.

Upvotes: 2

Vidhyanand
Vidhyanand

Reputation: 5369

You need to load Custom cell like below code..

You need to check for nil condition also as initially we need not have any custom cell to load..

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

    }

Hope it helps you..!

Upvotes: 0

Related Questions