Failed to obtain a cell from its DataSource
IosObjective CUitableviewIos Problem Overview
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
Ios Solutions
Solution 1 - Ios
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.
Solution 2 - Ios
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.
Solution 3 - Ios
#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. [1]: https://i.stack.imgur.com/cckMp.png [![enter image description here][1]][1]
Solution 4 - Ios
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.
Solution 5 - Ios
I had the same issue and it was because I forgot to implement UITableViewDataSource and UITableViewDelegate protocols
just add them to your class declaration after the inherited class
xcode must warn me about that, because I used UITableViewDataSource and UITableViewDelegate methods in my code
Solution 6 - Ios
In my case, I was forgot to declare UITableViewDataSource, UITableViewDelegate for UITableview.
Solution 7 - Ios
in my case, I forgot to change from dynamic prototype to static cell, check this part in your interface builder
Solution 8 - Ios
Careful when you are migrating
In case anyone is migrating their code from older swift versions to newer ones, this may occur if you unintentionally don't change UITableViewDataSource
functions' syntax to the newer swift syntax.
Unfortunately the compiler won't complain about it.
I faced this when I was migrating my code from swift 2 to swift 4. I didn't change cellForRow DataSource method syntax from:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
to its swift 4 version:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
and I got the error:
"Failed to obtain a cell from its DataSource"
Solution 9 - Ios
Also please try the below code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
// Reuse and create cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Update cell data contents
cell.textLabel.text = @"Your text here";
cell.detailTextLabel.text=@"Your detailed text label";
return cell;
}
Solution 10 - Ios
After having done the steps suggested in the other posts, and checking if my cell was nil, it my problem was not resolved and my cell was not showing as nil when I checked it. However, adding this handy code snippet in there did resolve the issue, so it must have been nil at some point where I wasn't catching it.
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ClassNameOfMyCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
Solution 11 - Ios
Check if have added UITableViewDataSource were added. I missed this, so that app was crashing
Solution 12 - Ios
Make sure you have not forgotten to implement the UITableViewDelegate and UITableViewDataSource Protocols.
class ExampleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
Solution 13 - Ios
Probably you are not using the same Identifier you have given in the main story board for prototype cell. Try to telly the name for Upper case lower case as well to make sure you are using correct identifier.
Good Luck:)
Solution 14 - Ios
If you are not loading Nib then you have to first register Nib in viewDidLoad.
[self.tableView registerNib:[UINib nibWithNibName:@"RecruitmentDetailViewC" bundle:nil] forCellReuseIdentifier:@"RecruitmentDetailViewC"];
Solution 15 - Ios
When using tableViewCell apart from the UITableView you have to register the NIB yourself. When the NIB is not registered the "Failed to obtain a cell from its DataSource" error may occur. From the error description I always find it hard to figure out, what is wrong.
This error can easily be solved by registering:
[self registerNib:[customerButtonTableViewCell class]
forCellReuseIdentifier:customerButtonCellIdentifier forTableView:tableView];
Solution 16 - Ios
You must override 'cellForRowAt' method. Take care if you didn't do it
Solution 17 - Ios
I know this question is quite old already, but for my end, the problem was that the cell isn't part of the target membership.
So the fix I did was to include it in the target membership.