9

In iOS5, using ARC and prototype cells for tableView on storyboard, can I replace the code below:

static NSString *CellIdentifier = @"Cell";

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

// Configure the cell...
return cell;

With this simple code??:

UITableViewCell *cell = [tableView 
  dequeueReusableCellWithIdentifier:@"Cell"];
return cell;

I saw this on this link:

http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

Thank's in advance!

Arildo

Arildo Junior
  • 796
  • 1
  • 8
  • 15

2 Answers2

8

Sure, your code are right, storyboard automaticaly alloc new cells, this code work great:

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

    //Configure cell
    //[cell.lab1 setText:@"Test"];

    return cell;
}
Kappe
  • 8,342
  • 2
  • 27
  • 35
  • 1
    I don't get why, but this thing doesn't work for me. I keep getting a "nil" cell. I create a new master-detail project. The example works great. When I add the cellForRowIndexPath method and table size method and set the size to be 2 I get an exception, since the dequeueReusableCellWithIdentifier keep getting me "nil". – bashan Jan 03 '12 at 21:57
  • 1
    have you configured the tableCell in the storyboard? Like this: [link]http://minus.com/m59pfEOqW (note: the cell identifier is the same in storyboard and in cellForRowAtIndexPath) – Kappe Jan 04 '12 at 11:08
  • 2
    And remember you should do any setup stuff in the cell subclass in the "awakeFromNib" method, not "initWithStyle:" (it doesn't get called) since it loads from the storyboard. – avocade Jan 08 '12 at 17:35
3

This is the way Apple intends it to be used, but I recommend against it. There is a bug that causes dequeueReusableCellWithIdentifier to return nil when VoiceAssist is enabled on a device. That means your app will crash for users with this option turned on. This is still a problem as of iOS 5.1.1

You can find more info and a workaround here:

http://hsoienterprises.com/2012/02/05/uitableview-dequeuereusablecellwithidentifier-storyboard-and-voiceover-doesnt-work/

The last paragraph has the work-around

bgolson
  • 3,330
  • 4
  • 22
  • 41