ios - 将 xib 文件作为 UITableViewController 中的 header 添加到 UITableView

标签 ios objective-c uitableview

我有一个 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/

相关文章:

ios - 我需要为 uitableview 性能 ipad 做更多的优化

ios - 滑动表格单元格以在 Swift 中执行操作

ios - 如何在 iOS 中重复播放一首歌曲?

ios - 使用未声明的类型 MGSwipeTableCell

java - 带有 JDBC 的 Gluon 示例项目在 iOS 设备上不起作用

ios - 在纵向和横向中为 UIView 设置适当的约束

objective-c - 为什么这个对象释放不正常,我应该释放它吗?

iphone - 异常 <UITextField>,应用程序崩溃

ios - 在 objective-c 中使用全局变量

iOS更改单元格边框色调颜色