我有一个 UITableViewController
的子类,我的表已经准备好了。
我的问题是,我想添加一个自定义 UIView
作为表格的标题。所以我创建了一个空的 xib 文件“HeaderView.xib”并在其中添加了我的 View (一个 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;
我不知道这是否是正确的方法,因为我在运行应用程序时看不到标题
谢谢你的帮助
最佳答案
您为 headerView
实现 getter 的方式将始终返回 nil
。像这样的东西应该表现得更恰当:
- (UIView *)headerView {
if (!_headerView) {
_headerView = [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil].firstObject;
}
return _headerView;
}
您实际上需要将 _headerView
设置为从 loadNibNamed
返回的 View 数组中的第一个对象。
关于ios - 将 xib 文件作为 UITableViewController 中的 header 添加到 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52482414/