【发布时间】:2012-02-26 21:46:15
【问题描述】:
在 iOS5 中,使用 ARC 和原型单元格用于 storyboard 上的 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
提前致谢!
【问题讨论】:
-
是的,你可以。此外,它不需要ARC。只要您使用的是 iOS 5 和故事板,就可以使用新模式。
-
@JasonCoco:这应该是一个答案,而不是评论。
-
@JasonCoco 我不明白为什么,但这东西对我不起作用。我不断得到一个“零”单元格。我创建了一个新的主从项目。该示例效果很好。当我添加 cellForRowIndexPath 方法和表大小方法并将大小设置为 2 时,我得到一个异常,因为 dequeueReusableCellWithIdentifier 一直让我“nil”。
-
@Moonlight 在你的故事板中,你需要确保你创建了一个原型单元,并且你在 IB 中给它的标识符和你在代码中所做的一样(在你的例子中,
Cell)。它区分大小写,因此必须将其命名为Cell,并带有大写字母“C”,并且您的故事板中应该只有一个具有该名称的原型单元格。 -
@JasonCoco 是的,我想我做了所有这些事情,但我仍然返回零单元格。这是一个演示代码ericyue.info/Circle.zip
标签: iphone objective-c ios5 automatic-ref-counting uistoryboard