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

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

问题描述

使用故事板,静态单元格,在cellForRowAtIndexPath:

Using storyboard, static cells, in cellForRowAtIndexPath: the line

UITableViewCell *cell = 
   [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

总是返回nil.

我检查了以下内容:

  • 在 IB/Storyboard 中正确设置了单元格的标识符,并且我在代码中使用了相同的标识符.我多次验证这一点.
  • 我没有在其他地方实例化视图控制器(这是这个stackoverflow问题中的问题).

我的视图控制器是 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?

推荐答案

在 table view 中使用静态内容,你没有实现任何数据源方法(包括 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天全站免登陆