简体   繁体   English

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

[英]Xcode 5.1 UITableView in UIViewController - Custom TableViewCell Outlets are nil

I have a UITableView as a subview in a UIViewController. 我有一个UITableView作为UIViewController中的子视图。 I'm not using UITableViewController because I have some other content that is not tableview-related taking up part of the screen. 我没有使用UITableViewController,因为我有一些与tableview无关的其他内容占用了部分屏幕。 I am using storyboards. 我正在使用故事板。 I have the tableview set up as an outlet, with the viewcontroller being the datasource and delegate. 我将tableview设置为插座,viewcontroller是数据源和委托。 If I use the standard UITableViewCell in this scenario, everything works well and I can load content. 如果我在这种情况下使用标准的UITableViewCell,一切运行良好,我可以加载内容。

I need to use a custom UITableViewCell for this tableview, so I performed the following steps: 我需要为此tableview使用自定义UITableViewCell,因此我执行了以下步骤:

  1. Created a UITableView subclass named SWTableViewCell 创建了一个名为SWTableViewCell的UITableView子类
  2. Added a call in viewDidLoad to register the cell with interface builder: [self.tableView registerClass:[SWTableViewCell class] forCellReuseIdentifier:@"Cell"]; 在viewDidLoad中添加了一个调用,用接口生成器注册单元格: [self.tableView registerClass:[SWTableViewCell class] forCellReuseIdentifier:@"Cell"];
  3. In the storyboard on my tableview, set the prototype cell's Class to SWTableViewCell 在我的tableview上的故事板中,将原型单元的Class设置为SWTableViewCell
  4. In the storyboard, set the cell reuse identifier to "Cell" 在故事板中,将单元重用标识符设置为“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 仍然在故事板中,按住Ctrl键从标签拖动到SWTableViewCell.h以创建出口
  7. In my cellForRowAtIndexPath: method, dequeue and cast the cell to the custom subclass: SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"]; 在我的cellForRowAtIndexPath:方法中,将单元格出列并转换为自定义子类: SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

The cell variable is the proper type when I check it. 检查时,单元格变量是正确的类型。 It's not a UITableViewCell, but a SWTableViewCell. 它不是UITableViewCell,而是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. 我敢肯定有一个我必须要忘记的联系,但我想不出我在这一点上跳过的任何事情。

Is this scenario possible, or does it just not work via IB and storyboards? 这种情况是否可行,或者它是否只能通过IB和故事板工作?

Removing the call to 删除呼叫

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

solves the issue. 解决了这个问题。

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 ! 如果有人有关于某些信息的信息,请告诉我们!


Edit : 编辑:

So, the idea behind registerClass:forCellReuseIdentifier: is that it tells the tableView how to create new cells. 因此, registerClass:forCellReuseIdentifier:背后的想法是它告诉tableView如何创建新的单元格。 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). 这样,如果dequeueReusableCellWithIdentifier:不返回单元格(如果队列为空),则tableView知道如何创建新单元格,而不需要手动创建一个单元格。


Edit 2 : 编辑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. 如果使用故事板中的原型创建单元格,则无需注册该类。

Simply calling dequeueReusableCellWithReuseIdentifier: will "activate" the cell reusing functionality of your tableView. 只需调用dequeueReusableCellWithReuseIdentifier:将“激活”tableView的单元重用功能。

So registerClass:forCellReuseIdentifier: is to be called only if you manually build your cells, without the use of storyboards. 所以registerClass:forCellReuseIdentifier:只有在不使用故事板的情况下手动构建单元格时才会被调用。

Here are some tips: 以下是一些提示:

Basically there can be 3 ways to create and use UITableViewCell s: 基本上可以有3种方法来创建和使用UITableViewCell

  1. Use “prototype” cells in the storyboard/nib 在storyboard / nib中使用“prototype”单元格

    (Create cell UI in the storyboard/nib inside UITableView ) (在UITableView内的storyboard / nib中创建单元格UI)

    You should not use either registerClass:forCellReuseIdentifier: or registerNib:forCellReuseIdentifier: methods. 不应该使用 registerClass:forCellReuseIdentifier:registerNib:forCellReuseIdentifier: methods。 UIStoryboard handles it on its own, since you specified the reuseIdentifier in the storyboard, the tableView knows which cell is associated with which reuseIdentifier . UIStoryboard自己处理它,因为你在storyboard中指定了reuseIdentifiertableView知道哪个cell与哪个reuseIdentifier相关联。

  2. Use a separate nib for tableViewCell UI tableViewCell UI使用单独的nib

    (This might be a way to go when you want to use the same tableViewCell in several tableView s) (当你想在几个tableView使用相同的tableViewCell时,这可能是一种方法)

    You should use registerNib:forCellReuseIdentifier: method. 应该使用registerNib:forCellReuseIdentifier:方法。

  3. Do not use nibs, create tableViewCell UI programmatically 不要使用nib,以编程方式创建tableViewCell UI

    You should use registerClass:forCellReuseIdentifier: method. 应该使用registerClass:forCellReuseIdentifier:方法。

Also, note that both registerClass:forCellReuseIdentifier: and registerNib:forCellReuseIdentifier: should be called before (most often in viewDidLoad ) dequeueReusableCellWithIdentifier:forIndexPath: method is called. 另请注意, registerClass:forCellReuseIdentifier:registerNib:forCellReuseIdentifier:都应该在之前调用(最常见于viewDidLoaddequeueReusableCellWithIdentifier:forIndexPath:方法被调用。 If you registered a class or a nib for the reuseIdentifier it is guaranteed that dequeueReusableCellWithIdentifier:forIndexPath: returns a valid cell (unlike dequeueReusableCellWithIdentifier: ). 如果为reuseIdentifier注册了类或nib,则可以保证dequeueReusableCellWithIdentifier:forIndexPath:返回一个有效的单元格(与dequeueReusableCellWithIdentifier:不同)。

In your code Storyboard registered a nib with UI for the cell, but you calling registerClass:forCellReuseIdentifier: with the same reuseIdentifier forced table view to unregister the nib and register the class which was not aware of IBOutlet s. 在您的代码中, Storyboard为单元格注册了一个带有UI的nib ,但是您调用了registerClass:forCellReuseIdentifier:使用相同的reuseIdentifier强制表视图取消注册nib并注册不知道IBOutlet的类。

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

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