UIViewController中的Xcode 5.1 UITableView - 自定义TableViewCell Outlets为零

问题描述:

我有一个UITableView作为UIViewController中的子视图。我没有使用UITableViewController,因为我有一些与tableview无关的其他内容占用了部分屏幕。我正在使用故事板。我将tableview设置为插座,viewcontroller是数据源和委托。如果我在这种情况下使用标准的UITableViewCell,一切都运行良好,我可以加载内容。

I have a UITableView as a subview in a UIViewController. I'm not using UITableViewController because I have some other content that is not tableview-related taking up part of the screen. I am using storyboards. I have the tableview set up as an outlet, with the viewcontroller being the datasource and delegate. If I use the standard UITableViewCell in this scenario, everything works well and I can load content.

我需要为此tableview使用自定义UITableViewCell,因此我执行了以下步骤:

I need to use a custom UITableViewCell for this tableview, so I performed the following steps:


  1. 创建了一个名为SWTableViewCell的UITableView子类

  2. 在viewDidLoad中添加了一个调用,用接口生成器注册单元格: [self.tableView registerClass:[SWTableViewCell class] ] forCellReuseIdentifier:@Cell];

  3. 在我的tableview的故事板中,将原型单元格的类设置为SWTableViewCell

  4. 在故事板中,将单元格重用标识符设置为单元格

  5. 在故事板上的原型单元格中添加了一些标签

  6. 仍然在故事板,从标签拖动到SWTableViewCell.h以创建出口

  7. 在我的 cellForRowAtIndexPath:方法中,出列并转换单元格到自定义子类: SWTableViewCell * cell =(SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@Cell];

  1. Created a UITableView subclass named SWTableViewCell
  2. Added a call in viewDidLoad to register the cell with interface builder: [self.tableView registerClass:[SWTableViewCell class] forCellReuseIdentifier:@"Cell"];
  3. In the storyboard on my tableview, set the prototype cell's Class to SWTableViewCell
  4. In the storyboard, set the cell reuse identifier to "Cell"
  5. Added some labels to the prototype cell on the storyboard
  6. Still in the storyboard, ctrl-dragged from the labels to the SWTableViewCell.h to create outlets
  7. In my cellForRowAtIndexPath: method, dequeue and cast the cell to the custom subclass: SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

T.当我检查它时,单元格变量是正确的类型。它不是UITableViewCell,而是SWTableViewCell。但没有一个网点填充。属性存在,但它们是零。我肯定有一个我必须要忘记的连接,但我想不出我此时跳过的任何内容。

The cell variable is the proper type when I check it. It's not a UITableViewCell, but a SWTableViewCell. But none of the outlets are populated. The properties exist, but they are nil. I'm sure there's a connection I must be missing, but i can't think of anything I skipped at this point.

这种情况是否可行,或者只是不通过IB和故事板工作?

Is this scenario possible, or does it just not work via IB and storyboards?

删除对

[self.tableView registerClass:[SWTableViewCell class] forCellReuseIdentifier:@"Cell"];

解决了这个问题。

我不喜欢我不知道这个方法到底做了什么,因为我从来没有用过它,所以我会调查一下。如果有人有这方面的信息需要这个电话,请告诉我们!

I don't know exactly what this method does as I've never used it, so I'll look into it. If anyone has information on wether or not this call is required for something, please tell us !

编辑:

因此, registerClass:forCellReuseIdentifier:背后的想法是告诉tableView如何创建新单元格。这样,如果 dequeueReusableCellWithIdentifier:没有返回一个单元格(如果队列为空),则tableView知道如何创建新单元格,而不需要手动创建一个)。

So, the idea behind registerClass:forCellReuseIdentifier: is that it tells the tableView how to create new cells. That way, if dequeueReusableCellWithIdentifier: does not return a cell (if the queue is empty), the tableView knows how to create a new cell, and you don't need to manually create one).

编辑2:

我发现主题,其中包含:

I found this thread, in which it says :


如果在故事板中使用原型创建单元格,则无需注册该类。

If the cell is created using prototypes within a storyboard it is not necessary to register the class.

只需调用 dequeueReusableCellWithReuseIdentifier:将激活tableView的单元重用功能。

Simply calling dequeueReusableCellWithReuseIdentifier: will "activate" the cell reusing functionality of your tableView.

所以 registerClass:forCellReuseIdentifier:只有在不使用故事板的情况下手动构建单元格时才会被调用。

So registerClass:forCellReuseIdentifier: is to be called only if you manually build your cells, without the use of storyboards.