sam sam - 9 months ago 54
Objective-C Question

UITableView with .xib cell with error : failed to obtain a cell from its dataSource

I have create table cell with .xib file. and added in the UITableView using following code :

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"];

if (!cell) {
[tableView registerNib:[UINib nibWithNibName:@"DisplayCell" bundle:nil] forCellReuseIdentifier:@"desplayCell"];
}

//
return cell;
}


App crashes with error :


failed to obtain a cell from its dataSource

Answer Source

I forget to add dequeueReusableCellWithIdentifier in your code.

dequeueReusableCellWithIdentifier usage

You should use the same reuse identifier for all cells of the same form. The reuse identifier is associated with those cells (rows) of a table view that have the same general configuration, minus cell content.

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"];

    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"DisplayCell" bundle:nil] forCellReuseIdentifier:@"desplayCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"];
    }

   //
 return cell;

}