0

I want to create UITableView with storyboard as the pic shows:

enter image description here

and the delegate methods in ITViewController.m is as below:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ITTableViewCell *cell = (ITTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ITTableViewCell"];

    return cell;
}

When I ran my app, I got the error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

It seems that no cell has been initialized, how can I fix this problem? thks!

itenyh
  • 1,921
  • 2
  • 23
  • 38
  • 1
    your cell's identifier must be "ITTableViewCell" in your story board. – Rajesh Apr 08 '14 at 14:43
  • Storyboards make it very simple - all you need to check is that the reuse identifier specified in the storyboard for your `ITTableViewCell` is `@"ITTableViewCell"`. – Sergey Kalinichenko Apr 08 '14 at 14:47
  • @rajesh I have solved by setting the identifier. I did not see the identifier property because of somehow hidding the property list of tableviewcell. Thks! – itenyh Apr 09 '14 at 00:04

1 Answers1

-1

you can initialize cell like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"ITTableViewCell";       

     UTTableViewCell *cell = (UTTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
     }  

     return cell;
}
woulstoke
  • 34
  • 3
  • 1
    There is no need to have an if(cell==nil) clause if the cell is made in the storyboard -- it will never be nil (assuming you use the correct identifier). – rdelmar Apr 08 '14 at 17:14
  • yeah thats the old way of doing it, you no longer have to do that – meda Apr 08 '14 at 21:32