Does dequeueReusableCellWithIdentifier work with ARC?

不打扰是莪最后的温柔 提交于 2019-12-23 17:27:56

问题


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.



来源:https://stackoverflow.com/questions/9142817/does-dequeuereusablecellwithidentifier-work-with-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!