故事板静态单元格:dequeueReusableCellWithIdentifier返回nil [英] Storyboard static cells: dequeueReusableCellWithIdentifier returns nil

查看:229
本文介绍了故事板静态单元格:dequeueReusableCellWithIdentifier返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cellForRowAtIndexPath中使用故事板,静态单元格该行

UITableViewCell *cell = 
   [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

始终返回 nil

我检查了以下内容:


  • 正确设置了单元格的标识符在IB / Storyboard中,我在代码中使用相同的标识符。我多次验证了这一点。

  • 我没有在别处实例化视图控制器(这是这个stackoverflow问题)。

  • Identifier of the cell is correctly set in IB/Storyboard and I use the same identifier in code. I verified this many times.
  • I have not instantiated the view controller elsewhere (which was the problem in this stackoverflow question).

我的视图控制器是 UITableViewController 的子类,当然,包含在故事板中的 ad hoc 导航控制器。怀疑我的视图控制器以某种方式不知道故事板中定义的单元标识符,因为它可能是另一个实例,这里是代码实例化它。在 prepareForSegue:中,我使用

My view controller is a subclass of UITableViewController, of course, wrapped into an ad hoc navigation controller in storyboard. Suspecting that my view controller somehow does not know about the cell identifiers defined in storyboard because it might be another instance, here is the code the "instantiates" it. In prepareForSegue:, I use

CustomViewController *vc = [[[segue destinationViewController] 
   viewControllers] objectAtIndex:0];

此处完成的视图控制器的其他自定义(设置属性等)工作正常。

Other customizations of the view controller done here (setting properties etc.) works fine.

我使用的是静态单元格,因为节和行的数量不会改变,每个单元格都包含静态文本(以及其他要编辑的控件或文本字段)。

I am using static cells because the number of sections and rows does not change, and each cell contains static text (and other controls or text fields to be edited).

在我看来,这是一项非常常见的任务(在视图控制器的数据源方法中自定义故事板中的静态单元格)。我做错了什么?

It seems to me this is a very common task (customize static cells from storyboard in the view controller's datasource methods). What am I doing wrong?

推荐答案

对于表视图中的静态内容,您不实现任何数据源方法(包括 tableView:cellForRowAtIndexPath:,所以你永远不会将单元格出列。静态内容没有出列(无论如何你都可以参与其中)。

With static content in a table view, you do not implement any of the datasource methods (including tableView:cellForRowAtIndexPath:, so you would never dequeue the cells. There is no dequeuing for static content (that you can get involved in, anyway).

如果你想得到一个指向特定单元格的指针:

If you want to get a pointer to a particular cell:


  • 从中得到它表视图使用 cellForRowAtIndexPath:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


  • 有特定单元格的插座并直接自定义。

  • have an outlet to the specific cell and customise it directly.

    任何这些都可以在viewWillAppear或类似的事情中完成。

    Any of these things can be done in viewWillAppear or similar.

    如果你想完全拥有不同的骗局在您的单元格中的帐篷到故事板上找到的那个静态单元格可能不是正确的选择。您应该使用传统的数据源方法来使用动态原型(请注意,故事板中可以有多个原型)。

    If you want to have completely different content in your cells to that found on the storyboard then static cells probably aren't the right choice. You should use dynamic prototypes (note you can have multiple prototypes in the storyboard) with the traditional data source methods instead.

    这篇关于故事板静态单元格:dequeueReusableCellWithIdentifier返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆