Mord Fustang
Mord Fustang

Reputation: 1553

Default UITableview gives error or cell returns null

I am designing a custom prototype cell with lots of labels, textfields and buttons in it.

I created a tableview controller on storyboard with a prototype cell.I have added a objective-c tableviewcontroller class. And connected them. Default code added by Xcode gives error itself.

in.h

@interface CreaatePlistTableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource>

in.m

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...

        return cell;
    }

Error:

** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

I have changed the code to this it compiles but returns an empty cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // NOTE: Add some code like this to create a new cell if there are none to reuse
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }
    // Configure the cell...
    UILabel *labelMeetingDate = (UILabel *)[cell viewWithTag:11];
    labelMeetingDate.text = @"Meeting Date";

    return cell;
}

Section and rows are set to 1 btw.

How can I add labels and textfields to prototype cell, or why it gives error or returns null?

Upvotes: 4

Views: 7076

Answers (1)

Michael Kernahan
Michael Kernahan

Reputation: 1462

You need to set the cellIdentifier on the cell itself in the storyboard. Fourth tab over. Listed under the Table View Cell section property Identifier

Upvotes: 6

Related Questions