Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone dequeueReusableCellWithIdentifier是否与ARC一起工作?_Iphone_Objective C_Ios5_Automatic Ref Counting_Uistoryboard - Fatal编程技术网

Iphone dequeueReusableCellWithIdentifier是否与ARC一起工作?

Iphone dequeueReusableCellWithIdentifier是否与ARC一起工作?,iphone,objective-c,ios5,automatic-ref-counting,uistoryboard,Iphone,Objective C,Ios5,Automatic Ref Counting,Uistoryboard,在iOS5中,在情节提要上使用ARC和prototype单元格作为tableView,我是否可以替换以下代码: static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc]

在iOS5中,在情节提要上使用ARC和prototype单元格作为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;
我在这个链接上看到:


谢谢,这是事先准备好的

发生此问题是因为您没有从情节提要创建
MenuViewController
。您是这样创建的:

MenuViewController *menuViewController = [[MenuViewController alloc] init];
MenuViewController *menuViewController =  [self.storyboard instantiateViewControllerWithIdentifier:@"menuViewController"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
                                                          forIndexPath:indexPath];

cell = [cell initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
MenuViewController
的实例未连接到情节提要,因此它不知道情节提要中的原型单元

您需要进入故事板,将
MenuViewController
的标识符设置为类似
MenuViewController
的值。然后,您可以创建这样的实例:

MenuViewController *menuViewController = [[MenuViewController alloc] init];
MenuViewController *menuViewController =  [self.storyboard instantiateViewControllerWithIdentifier:@"menuViewController"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
                                                          forIndexPath:indexPath];

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

我的解决方案最终是这样的:

MenuViewController *menuViewController = [[MenuViewController alloc] init];
MenuViewController *menuViewController =  [self.storyboard instantiateViewControllerWithIdentifier:@"menuViewController"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
                                                          forIndexPath:indexPath];

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

因为从iOS5.0开始,第一行代码永远不会产生nil值,而且我没有看到其他方法来指定我想要的样式。或者,我可以从库中添加一个Table View Controller实例,然后在prototype单元格中编辑样式。

是的,可以。而且,它不需要弧。只要你使用iOS 5和故事板,你就可以使用新模式。@JasonCoco:这应该是一个答案,而不是评论。@Jasoncoo我不明白为什么,但这个东西对我不起作用。我的手机一直是“零”。我创建了一个新的主细节项目。这个例子效果很好。当我添加cellForRowIndexPath方法和table size方法并将大小设置为2时,我得到一个异常,因为dequeueReusableCellWithIdentifier一直将我设置为“nil”。@Moonlight在故事板中,您需要确保您创建了一个原型单元,并且在IB中为它指定了与在代码中相同的标识符(在你的例子中,
Cell
)。它区分大小写,因此必须用大写字母“C”命名为
Cell
,并且你的故事板中应该只有一个同名的原型单元格。@Jasoncoo是的,我想我做了所有这些事情,但我仍然返回了nil Cell。这是一个演示代码,谢谢!它可以工作:)有没有办法直接从情节提要创建MenuViewController?从
菜单按钮按下:
方法断开书签按钮(在时间线导航栏中)。控件从书签按钮拖动到情节提要中的
MenuViewController
实例,并选择
Push
Modal
作为segue类型。不,这是低效的,并表明对单元格类与标识符的关联方式存在误解。如果出列方法没有返回
nil
,那是因为您(a)已经将情节提要单元原型与该标识符相关联;(b) 您使用了注册表类:forCellReuseIdentifier:;或者(c)您以前使用了
initWithStyle:reuseIdentifier:
,并且它成功地将单元格出列。现在,您通常会使用选项(a),并修改故事板,使单元原型具有适当的样式。然后你就可以删除第二行代码了谢谢Rob。我的代码是case(b),我找不到设置单元格样式的方法。如果使用了
registerClass
,则子类应该实现
initWithStyle
,它调用
[super initWithStyle:…]
,但明确指定适当的样式,而不是只传递提供的
UITableViewCellStyle
。但是上面的代码示例是不可取的,因为它会导致
initWithStyle
被调用两次(因为
dequeueReusableCellWithIdentifier
将调用
initWithStyle
本身,如果没有可重用的单元格,那么您将再次调用它)。