9

在 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

提前致谢!

阿里尔多

4

2 回答 2

8

当然,您的代码是正确的,情节提要自动分配新单元格,此代码工作得很好:

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

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

    return cell;
}
于 2011-11-06T16:37:47.413 回答
3

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

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

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

最后一段有解决方法

于 2012-05-30T23:03:30.753 回答