为原型单元格更改了dequeueReusableCellWithIdentifier行为?

时间:2011-10-30 18:22:04

标签: iphone ios5 tableview storyboard cells

在iOS5中,在故事板上使用ARC和原型单元格用于tableView,我可以替换下面的代码:

static NSString *CellIdentifier = @"Cell";

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

// Configure the cell...
return cell;

用这个简单的代码??:

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

我在这个链接上看到了这个:

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

提前感谢!

Arildo

2 个答案:

答案 0 :(得分:8)

当然,你的代码是正确的,故事板自动分配新的单元格,这段代码非常好用:

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

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

    return cell;
}

答案 1 :(得分:3)

这是Apple打算使用它的方式,但我建议不要这样做。在设备上启用VoiceAssist时,有一个错误导致dequeueReusableCellWithIdentifier返回nil。这意味着打开此选项的用户的应用会崩溃。从iOS 5.1.1开始,这仍然是个问题

您可以在此处找到更多信息和解决方法:

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

最后一段有解决方法