简体   繁体   English

静态单元格 - 插座始终为零

[英]Static cells - Outlets are always nil

It's the first time I use static cells in a table view and I just can't get them to show.这是我第一次在表格视图中使用静态单元格,但我无法让它们显示出来。

I have two sections (grouped) with two and three cells in each one respectively.我有两个部分(分组),每个部分分别有两个和三个单元格。 The cells are static (custom) and all carry one label and one textfield:单元格是静态的(自定义)并且都带有一个标签和一个文本字段:

在此处输入图片说明

I have created IBOutlets for each textfield, and for each cell in my view controller:我为每个文本字段以及视图控制器中的每个单元格创建了IBOutlets

在此处输入图片说明

I have made the link between storyboard -> view controller where the outlets reside:我已经建立了故事板 - > 插座所在的视图控制器之间的链接:

在此处输入图片说明

I have also set datasource & delegate on UITableView :我还在UITableView上设置了数据源和委托:

在此处输入图片说明

Since these are static cells, I am not using dynamic cell-related methods, eg tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) .由于这些是静态单元格,我没有使用与单元格相关的动态方法,例如tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) Instead, I try to reference each textfield and cell via their outlets.相反,我尝试通过它们的出口引用每个文本字段和单元格。 My problem is that I keep getting a nil value returned for all the outlets.我的问题是我一直在为所有网点返回一个 nil 值。 I tried referencing them in viewDidLoad , viewWillAppear and viewDidAppear but the result is always nil:我尝试在viewDidLoadviewWillAppearviewDidAppear引用它们,但结果始终为零:

在此处输入图片说明

I programmatically instantiate the UIViewController via the use of a cocoa menu control:我通过使用可可菜单控件以编程方式实例化 UIViewController:

在此处输入图片说明

What on earth am I missing?我到底错过了什么? Any clues?有什么线索吗?

  • Note: I am Swift 2.1, Xcode 7.2注意:我是 Swift 2.1,Xcode 7.2

The problem is actually obvious.问题其实很明显。 It's like you have not instantiated it.就好像你还没有实例化它。 That's why it's all nil and you can't use it.这就是为什么它全部nil并且您不能使用它。 Since you are using Nib file, you will need to have a way to load the Nib file and then reference it.由于您使用的是Nib文件,因此您需要有一种方法来加载Nib文件然后引用它。 When you are accessing this UIViewController from Storyboard, the system will take care this for you.当您从 Storyboard 访问此UIViewController时,系统会为您处理。 What I am suspecting is, you programmatically create this UIViewController .我怀疑的是,您以编程方式创建了这个UIViewController

From another prospective, tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) is not for dynamic cells only.从另一个角度来看, tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)不仅仅适用于动态单元格。 You can pass in the total number of your static cells and you can still use this method to perform the operation you want.您可以传入静态单元格的总数,您仍然可以使用此方法执行您想要的操作。

The problem was that I wasn't instantiating the UIViewController from the Storyboard.问题是我没有从 Storyboard 实例化 UIViewController。

Before (all outlets were nil):之前(所有网点都为零):

在此处输入图片说明

After (worked):之后(工作):

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM