0

So I have custom UITableViewCells (made with storyboard), and when I do not literally alloc, init the cells, the app crashes. However, since they are custom cells, I don't know how to alloc/init them.

This code crashes:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:@"TableViewCellsID"];

return cell;
}

with this printed to the console:

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

I understand this means its crashing because there is no cell allocated yet, however when I try to allocate it, I have to specify a cell style.

So, when I build the cell as follows, it doesn't crash:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     TableViewCells *cell = (TableViewCells *)[tableView  dequeueReusableCellWithIdentifier:@"TableViewCellsID"];
     if (cell == nil)
          cell = [[TableViewCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TableViewCellsID"];

     return cell;
}

The problem is the code that works (the second set), requires me to set a UITableViewStyle. And as far as I know, there is no UITableViewStyleCustom.

Any helps would be appreciated.

Andrew
  • 3,874
  • 5
  • 39
  • 67

1 Answers1

-1

It crashes in first case because there is no cell to reuse, and you are calling dequeueReusableCellWithIdentifier.

EDIT - For custom cell initialisation you can check following threads -

How to make custom TableViewCell with initWithStyle after 3.0

initWithFrame vs initWithStyle

Community
  • 1
  • 1
rishi
  • 11,779
  • 4
  • 40
  • 59
  • I understand why its crashing, but I need a way to create a cell, without specifying a style – Andrew Jun 28 '12 at 15:52
  • why down vote? the problem which is mentioned in the code is crash, i provided the reason.Now for the second query i will give the answer. – rishi Jun 28 '12 at 15:55
  • If you interested in custom cell alloc, then you can check this thread - http://stackoverflow.com/questions/1754036/how-to-make-custom-tableviewcell-with-initwithstyle-after-3-0 – rishi Jun 28 '12 at 15:56
  • Also check http://stackoverflow.com/questions/9380710/initwithframe-vs-initwithstyle – rishi Jun 28 '12 at 15:58