我有一个UITableViewController
的子类,并且我的表已经准备好了。
我的问题是,我想添加一个自定义UIView
作为表的标题。所以我创建了一个空的xib文件“ HeaderView.xib”,并在其中添加了我的视图(一个UIView
和2个UIButton
),并添加了UITableViewController的类作为文件所有者,并连接了两个按钮上课:
- (IBAction)addNewItem:(id)sender
{
}
- (IBAction)toggleEdittingMode:(id)sender
{
}
在类扩展中,我添加了UIView类型的属性:
@property (nonatomic, strong) IBOutlet UIView *headerView;
然后我覆盖了getter方法:
- (UIView *)headerView
{
if (_headerView) {
[[NSBundle mainBundle] loadNibNamed:@"HeaderView"
owner:self
options:nil];
}
return _headerView;
}
并在viewDidLoad中:
self.tableView.tableHeaderView = self.headerView;
我不知道这是否是正确的方法,因为我在运行应用程序时看不到标题
感谢您的帮助
答案 0 :(得分:3)
您为headerView
实现getter的方式将始终返回nil
。这样的行为应该更合适:
- (UIView *)headerView {
if (!_headerView) {
_headerView = [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil].firstObject;
}
return _headerView;
}
您实际上需要将_headerView
设置为从loadNibNamed
返回的视图数组中的第一个对象。