4
votes

Implemented UITableView with custom UITableViewCell with below code

    @interface ProfileSaveViewController : UITableViewController
     {
         UITableViewCell *cell0;
         UITableViewCell *cell1;
         UITableViewCell *cell2;
         UILabel *cell2Label;
     }

    @property (nonatomic, retain) IBOutlet UITableViewCell *cell0;
    @property (nonatomic, retain) IBOutlet UITableViewCell *cell1;
    @property (nonatomic, retain) IBOutlet UITableViewCell *cell2;
    @property (nonatomic, retain) IBOutlet UILabel *cell2Label;

    @end

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:               (NSIndexPath*)indexPath
  {
        if([indexPath row] == 0) return cell0;
        if([indexPath row] == 1) return cell1;
        if([indexPath row] == 2) return cell2;   
        return nil;
  }

follwing message came when running the application.

* Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072 2012-03-01 11:11:31.984 TestSplitView[765:f803] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

5

5 Answers

8
votes

You're being asked for a cell and returning nil (or maybe something else that's not a UITableViewCell). It's just that simple.

I see two possibilities:

  • You are returning a number larger than 3 from tableView:numberOfRowsInSection:. Don't.

  • One or more of your cell# outlets is not hooked up in your nib, or is not hooked up to a UITableViewCell (or subclass). Hook them up properly.

4
votes

In case you're using a custom UITableViewCell, is also possible that you forgot to identify "Identifier" on "Utilites side" -> "Attribute Inspector" tab.

Hope this helps.

1
votes
0
votes
/* Dynamic Prototype cell in UITableView */

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView ==tblUser) {
        switch (indexPath.row)
        {
            case 0:
                return Cell_0;
                break;
            case 1:
                return Cell_1;
                break;
            case 2:
                return Cell_2;
                break;
            case 3:
                return Cell_3;
                break;
            case 4:
                return Cell_4;
                break;
            case 5:
                return Cell_04;
                break;
            case 6:
                return Cell_5;
                break;
            case 7:
                return Cell_6;
                break;
            case 8:
                return Cell_7;
                break;
            default:
                return nil;
                break;
        }
    }else
return nil
}
0
votes

Your should check your tableview datasource method for number of rows. Make sure that

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

If you are returning more than that, then your cellForRow would return nil, which tableview datasource expects UITableViewCell instance, due to that assertion fails.