Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to obtain a cell from its DataSource

What is wrong with the below code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *CellIdentifier  = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell.textLabel.text = @"hello";

    return cell;
}

And

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

but I'm getting failed to obtain a cell from its dataSource

The entire exception is

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x7ffe0b092800; frame = (0 97; 414 590); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ffe0acf3b10>; layer = <CALayer: 0x7ffe0aceb6e0>; contentOffset: {0, 0}; contentSize: {414, 88}>) failed to obtain a cell from its dataSource (<AllContactsViewController: 0x7ffe0ace60f0>)'

I have set delegate and data source for the table view

like image 597
Rishab Avatar asked Dec 13 '15 11:12

Rishab


4 Answers

Using Swift 3 I encountered this error when I forgot to add the UITableViewDataSource and UITableViewDelegate protocols to the ViewController declaration.

class DataViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

Adding them fixed the problem.

like image 145
David Reidy Avatar answered Nov 01 '22 05:11

David Reidy


Your error suggests that cellForRowAtIndexPath is returning nil for some reason, and I'm guessing it's because you are failing to dequeue a reusable cell. If you want to confirm this, just set a breakpoint after your current dequeue call: I expect you'll find cell is set to nil.

If you're using modern Xcode templates where you get a prototype cell made for you, you should probably be using this instead:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

If you aren't using an Xcode template, use that line of code anyway then register your own re-use identifier like this:

[self.tableView registerClass:[UITableViewCell self] forCellReuseIdentifier:@"Cell"];

All being well that should resolve the problem. I wrote this up in more detail for Swift users.

like image 25
TwoStraws Avatar answered Nov 01 '22 05:11

TwoStraws


Swift 3.0

You just need to add UITableViewDelegate and UITableViewDataSource

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

Below image is just for your reference.

enter image description here

like image 43
Vivek Avatar answered Nov 01 '22 06:11

Vivek


You have to give the cell an identifier. "Cell" give to the identifier of cell as in the attributes inspector of cell in the identifier field. I re-produced your error and it is due to you have not given an identifier to your cell.

like image 3
Muhammad Zohaib Ehsan Avatar answered Nov 01 '22 05:11

Muhammad Zohaib Ehsan