将UIView放入UITableView标头中 [英] Put a UIView into a UITableView Header

查看:95
本文介绍了将UIView放入UITableView标头中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,它位于我的xib文件中。我为控制器创建了这样的属性:

I have a UITableView which is in my xib file. And I created a property like this for the controller:

@property (nonatomic, retain) IBOutlet UITableView *myTableView;

现在,我希望有一个表格视图标题(不是每个部分的标题)。所以,因为我想要自定义样式,我创建了一个带有视图的新xib文件(我连接到我的控制器,实现了 myTableView 。)

Now, I want to have a table view header (not a header for each section). So, because I want to have custom styling, I created a new xib file with a view (and I connected to my controller which has the myTableView implemented).

然后我可以在我的控制器中写入 viewDidLoad

Then I can write in viewDidLoad in my controller:

[self.myTableView setTableHeaderView:self.myTableHeaderView];

其中 myTableViewHeader 是一个UIView属性控制器。

where myTableViewHeader is a UIView property in the controller.

不幸的是,UITableView不会显示这个UIView,所以我的问题是,如何将UIView放到UITableView中?

Unfortunately, the UITableView won't display this UIView, so my question is, how can I put a UIView to a UITableView into the header?

提前谢谢你&最好的问候。

Thank you in advance & Best Regards.

推荐答案

要检查的几件事情:


  • myTableHeaderView也必须是IBOutlet或在您的类中的代码中创建

  • myTableHeaderView must also be an IBOutlet or created in code in your Class somewhere

self.myTableHeaderView不能为零您正在尝试将其添加为表标题视图。如果它是零,那么你没有正确地连接你的插座

self.myTableHeaderView must not be nil when you're trying to add it as the table header view. If it is nil then you didn't hook up your outlets correctly

如果你在IB的自己的.xib文件中设计了表视图头,然后你必须在某个地方调用它(viewDidLoad是一个好地方):

if you've designed the table view header in IB in its own .xib file, then you must somewhere call this (viewDidLoad is a good place):

[[NSBundle mainBundle] loadNibNamed:@"MyTableViewHeader" owner:self options:nil];

MyTableViewHeader.xib的文件所有者必须是你的TableViewController子类,你必须连接myTableHeaderView反对文件所有者的相应插座。

The File's Owner of the MyTableViewHeader.xib must be your TableViewController subclass, and you must hook up the myTableHeaderView object to File's Owner's corresponding outlet.

编辑:回答什么 [[NSBundle mainBundle] loadNibNamed:@MyTableViewHeaderowner:self选项:nil]; 做什么?

in answer to "what does [[NSBundle mainBundle] loadNibNamed:@"MyTableViewHeader" owner:self options:nil];" do?

这一条小线包含的神奇之处将会让你睁开眼睛看看XIB文件和Objective-C对象在Cocoa(touch)中协同工作,将您的iOS编程功能提升到全新的水平。有两类Cocoa程序员,他们了解它的作用和使用并从中受益,以及那些还不知道他们缺少什么的人,而是偶然发现他们试图为他们的对象创建XIB文件。从来没有让它发挥作用。

This one little line contains the magic that will open your eyes to how XIB files and Objective-C objects work together in Cocoa (touch), elevating your iOS programming kung-fu to entirely new levels. There are two classes of Cocoa programmers, those who understand what it does and use and benefit from it, and those who don't yet know what they're missing, and instead stumble through the wilderness trying to create XIB files for their objects and never quite getting it to work.

这里的大量积累是详细信息:

With that massive build-up here's the details:

A XIB( NIB)文件是归档的Objective-C对象的集合(并且引用实际上不在XIB内的对象,如文件所有者,所谓的代理对象)以及这些对象之间的连接。加载XIB文件时,这些归档对象将完全按照它们保存到XIB中的状态生效,然后这些活动对象(和代理对象)之间的连接是根据记录在XIB文件中的连接。

A XIB (NIB) file is a collection of archived Objective-C objects (and reference to objects not actually within the XIB, like "File's Owner", a so-called "proxy object") and connections between these objects. When a XIB file is loaded, these archived objects are brought to life in exactly the state they were saved into the XIB, and then the connections between those live objects (and "proxy objects") are made according to the connections recorded in the XIB file.

例如,在标准的UIViewController子类.xib文件中,将File的Owner设置为MyViewController类。 .xib内部是一个UIView对象,它通常本身包含其他UIKit对象。 UIViewController类的view出口设置为指向.xib中的UIView对象。加载此.xib文件时,UIView对象将被取消归档并成为活动的UIView对象,其中包含.xib中记录的所有属性和设置。这就是非归档部分。

For example, in your standard UIViewController subclass .xib file, you have File's Owner set to your MyViewController class. Inside the .xib is a UIView object, which usually itself contains other UIKit objects. The "view" outlet of the UIViewController class is set to point to the UIView object in the .xib. When this .xib file is loaded, the UIView object is unarchived and becomes a living UIView object, with all of the properties and settings recorded in the .xib. That's the "unarchiving part".

然后,因为.xib从File的所有者(MyViewController类)到UIView对象的连接,指向这个新的指针UIView对象存储在MyViewController实例的view字段中。 .xib文件中也存在的任何其他连接,如UILabels,UIButton操作等,也会设置到MyViewController中的任何其他IBOutlet字段。

And then, because of the connection in the .xib from File's Owner (MyViewController class) to the UIView object, the pointer to this new UIView object is stored in the "view" field of your MyViewController instance. Any other connections also present in the .xib file, like UILabels, UIButton actions, etc., are also set up, to any other "IBOutlet" fields in MyViewController.

这一切看起来都很神奇,并且发生在

This all seems like magic and happens with the

(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle

方法。

现在好的部分:你可以自己做这种与对象关联的nib文件!您可以使用 loadNibNamed:owner:options 方法将关联任何.xib文件与您想要的任何对象中的匹配的nil IBOutlet组合 !! !

Now the good part: you get to do this sort of associating nib files with objects yourself! You can use the loadNibNamed:owner:options method to associate any .xib file with matching set of nil IBOutlets in any object that you want!!!

突然之间,创建完全自定义的表格视图单元格,表格页眉,页脚,等等,都是轻而易举的,您可以轻松编写模块化可重复使用的UIView组件等。 ,所有在Interface Builder中布局。

All of a sudden, creating entirely custom table view cells, table headers, footers, whatever, is a breeze, you can easily write modular reusable UIView components, etc., all laid out in Interface Builder.

要使用从.xib文件加载的对象填充其nil IBOutlets的对象是所有者对象。通常(但我不确定这是绝对必需的,任何类具有相同类型且命名的IBOutlets设置为File的所有者可能有效),这是将被指定为文件所有者的类在xib。

The object whose nil IBOutlets you want to fill in with objects loaded from a .xib file is the "owner" object. Usually (but I'm not sure this is absolutely required, any class with the identically typed and named IBOutlets set to File's Owner may work), this is the class that will be specified as "File's Owner" in the xib.

好了,现在你已经拥有了nil IBOutlets的现有所有者对象(IBOutlets必须是nil或者它们不会被更改,这就是将XIB加载到所有者对象中的规则。一些IBOutlet不是nil是正常的,加载XIB文件时它们不会被更改,通常这就是你想要的,并且你有.xib文件使用要加载到所有者对象的对象。你打电话:

OK, now you've got your existing owner object with nil IBOutlets (the IBOutlets must be nil or they won't be changed, that's the "rule" of loading XIBs into an owner object. It's ok that some IBOutlets are not nil, they just won't be changed when you load the XIB file, and usually that's what you want), and you've got your .xib file with objects that you want to load into the owner object. You call:

[[NSBundle mainBundle] loadNibNamed:@"MyXIBFileToLoad" owner:theOwner options:nil];

..瞧!现在,连接到MyXIBFileToLoad.xib中的对象的theOwner中的所有nil IBOutlets都已设置为从XIB文件加载的新对象。

.. and voila! Now any nil IBOutlets in "theOwner" that are connected to objects in the MyXIBFileToLoad.xib have been set to the new objects loaded from the XIB file.

(另外,这个method返回从xib取消归档的所有对象的数组。如果你不关心设置任何所有者的出口,你可以按类和标记搜索这个列表中的对象。

(also, this method returns an array of all objects unarchived from the xib. If you don't care about setting any owner's outlets, you can just search this list for your objects by class and by tag).

所以这就是故事,现在开始疯狂地将Objective-C类与存储在XIB文件中的对象相关联的新方法!

So that's the story, now go crazy with new ways to associate Objective-C classes with objects stored in XIB files!

这篇关于将UIView放入UITableView标头中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆