Does dequeueReusableCellWithIdentifier work with A

2019-07-13 11:36发布

问题:

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!

回答1:

This problem is happening because you aren't creating the MenuViewController from the storyboard. You are creating it like this:

MenuViewController *menuViewController = [[MenuViewController alloc] init];

That instance of MenuViewController isn't connected to the storyboard, so it doesn't know about the prototype cells in the storyboard.

You need to go into your storyboard and set the identifier of the MenuViewController there to something like menuViewController. Then you can create an instance like this:

MenuViewController *menuViewController =  [self.storyboard instantiateViewControllerWithIdentifier:@"menuViewController"];


回答2:

My solution was finally like this:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
                                                          forIndexPath:indexPath];

cell = [cell initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

Because form iOS 5.0 onwards the first line of code will never produce a nil value, and I saw no other way to specify the style I wanted. Or, I could have Added an instance of Table View Controller from the library, and then I could edit the style in the prototype cell.