Failed to obtain a cell from its DataSource

Rishab picture Rishab · Dec 13, 2015 · Viewed 65.6k times · Source

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

Answer

David Reidy picture David Reidy · Jul 27, 2016

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.