[英]How to add Storyboard headerview to tableview created at initialization by UITableViewController
I have a subclass of UITableViewController. 我有UITableViewController的子类。
By default, it creates a UITableView
when it is initialized. 默认情况下,它在初始化时创建一个UITableView
。 To that tableview I have set a header that I created in Interface Builder in the screen that is controlled by the controller. 对于该表视图,我设置了在控制器控制的屏幕上在Interface Builder中创建的标题。 The header has two buttons: 标题有两个按钮:
one to enter editing mode for the tableview (called "Edit") 一种进入表格视图的编辑模式(称为“编辑”)
one to add a random item to the tableview (called "New"). 一个向表视图添加随机项(称为“新建”)。
I linked an IBOutlet
property called headerView
to the header from Interface Builder and I set it to be the header of the UITableView
created at initialization in the viewDidLoad
method. 我将名为headerView
的IBOutlet
属性headerView
到Interface Builder的标头,并将其设置为在viewDidLoad
方法中初始化时创建的UITableView
的标头。
The problem is that when I press the "New" button (which adds a new row with a new item to the tableview) the header of the tableview falls down to the bottom of the tableview. 问题是,当我按下“新建”按钮(这会向表视图添加带有新项的新行)时,表视图的标题会下降到表视图的底部。
Any idea why? 知道为什么吗? How can I make it stick to the top? 如何使它坚持到底?
This is the viewDidLoad
method: 这是viewDidLoad
方法:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
[self.tableView setTableHeaderView:self.headerView];
}
This is the method that gets executed when the "New" button is pressed: 当按下“新建”按钮时,将执行以下方法:
- (IBAction)addNewItem:(id)sender {
Item *newItem = [[ItemStore sharedStore] createItem];
NSInteger lastRow = [[[ItemStore sharedStore] allItems] indexOfObject:newItem];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRow inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
Thanks. 谢谢。
Possibly you've set up the layout for table header view in a wrong way. 可能是您以错误的方式为表标题视图设置了布局。 Was there any layout constraint on it? 是否有任何布局限制?
By the way, this project on GitHub is a very simple example to demonstrate how simply it is to achieve what you want in the storyboard. 顺便说一句, GitHub上的这个项目是一个非常简单的示例,展示了在故事板上实现所需内容的简单程度。 It will produce the result like below: 它将产生如下结果:
Please use this project to compare with your current configuration. 请使用此项目与您当前的配置进行比较。 I hope this helps in one way or another. 我希望这会以某种方式有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.