本文介绍了UITableView registerNib:forCellReuseIdentifier:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WWDC视频中看到过这种情况,但只是非常简短。他们没有介绍如何创建实际的xib文件。



我有一个名为MyCustomCell的UITableViewCell子类。在这里我有几个属性UILabels,UIImageViews等...都设置为IBOutlets。



现在,在我的xib文件...



我该设置什么作为文件的所有者?我在哪里引用我的MyCustomCell类是文件的所有者?



一旦我设置了文件的所有者,我该如何将它与xib的根视图链接? / p>

我尝试过一些设置,但在使用时我一直都会遇到错误。



哦,代码我用来注册它是...

  self.cellNib = [UINib nibWithNibName:@MyCustomCellbundle:nil] ; 
[self.tableView registerNib:self.cellNib forCellReuseIdentifier:@CustomCell];

谢谢

解决方案

在这种情况下,通常你不必担心File的所有者,因为当 tableView 从提供的/关联的<$ c $实例化单元格时c> UINib 以及 reuseIdentifier 。它将加载nib的所有顶级对象,仅使用类 UITableViewCell 的第一个顶级对象(或者可能只是第一个顶级对象,不管是哪个类?但一般来说,你的XIB中只有你的 UITableViewCell - 不计算文件的所有者 First Responder 只是代理人。



事实上, tableView 将尝试将单元格出列,如果找不到可重复使用的单元格,它将使用 UINib 你提供的。它将与类似

  NSArray * topLevelObjects = [self.cellNib instantiateWithOwner:nil选项:0]; 
cell = [topLevelObjects objectAtIndex:0];

(这当然是一个简化版本只是为了表明这个想法,我不知道如果它实际上调用这些确切的行,但它应该非常接近)



所以 文件的所有者未使用,您只需将一个简单的自定义 UITableViewCell 作为唯一的顶级对象现有文件所有者旁边的XIB文件 anf 第一响应者(同样,仅限于此proxies/外部对象引用并且不会被实例化,也不会成为 instantiateWithOwner:options:)返回的顶级对象的一部分。






如果仍然不起作用:




  • 确保您正确填写了IB中 UITableViewCell reuseIdentifier (在您选择单元格后,在右侧的对象检查器窗格中) IB),并且在IB中使用与您在代码中使用的 reuseIdentifier 属性完全相同的值
  • 如果仍然没有运气,请提供更多信息,特别是您的错误,日志消息或例外情况。


I saw this used in a WWDC video but only very briefly. They didn't go in to how to create the actual xib file.

I've got a UITableViewCell subclass called MyCustomCell. In this I have several properties UILabels, UIImageViews, etc... all set up as IBOutlets.

Now, in my xib file...

What do I set as the file's owner? Where do I reference my MyCustomCell class is this the file's owner?

Once I've set the file's owner how do I link it with the root view of the xib?

I've tried a few settings but I keep getting errors when using it.

Oh, the code I'm using to register it is...

self.cellNib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];
[self.tableView registerNib:self.cellNib forCellReuseIdentifier:@"CustomCell"];

Thanks

解决方案

Normally you don't have to bother about the File's owner in that case, because when the tableView instantiates the cell from the provided/associated UINib along with the reuseIdentifier. It will load all the top-level objects of the nib, and use only the first top-level object that is of class UITableViewCell (or maybe just the first top-level-object regardless of the class? but in general you only have your UITableViewCell in your XIB anyway — without counting the File's Owner and the First Responder which are only "proxies").

In fact, the tableView will try to dequeue a cell and if it doesn't find a reusable one, it will create a new one using the UINib you provided. It will be something similar to this:

NSArray* topLevelObjects = [self.cellNib instantiateWithOwner:nil options:0];
cell = [topLevelObjects objectAtIndex:0];

(That's of course a simplified version just to show the idea, I don't know if it actually calls these exact lines, but it should be quite close)

So the File's Owner is not used in this particular case, and you only need to put a simple custom UITableViewCell as the only top-level-object of your XIB file next to the existing File's Owner anf First Responder (that, again, are only "proxies" / "External Objects references" and won't be instantiated and won't be part of the top-level-objects returned by instantiateWithOwner:options:).


If it still doesn't work:

  • Ensure that you correctly filled the reuseIdentifier of your UITableViewCell in IB (in the Object Inspector pane on the right once you selected your cell in IB), and used the exact same value for this reuseIdentifier property in IB that the one you use in your code.
  • If still no luck, please provide more info, especially what kind of error, log message or exception you have.

这篇关于UITableView registerNib:forCellReuseIdentifier:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:36