简体   繁体   English

如何将Storyboard标头视图添加到由UITableViewController初始化时创建的表视图

[英]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. 我将名为headerViewIBOutlet属性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];

}

从这一点开始,标题停留在表格视图的底部 这是tableview为空时标题的屏幕截图

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.

相关问题 故事板中的UITableViewController tableView大小 - UITableViewController tableView size in Storyboard 如何在不同的UItableViewController中重用在Storyboard中创建的UITableViewCell视图? - How reuse UITableViewCell view which is created in Storyboard in different UItableViewController? 使用storyboard将页脚添加到UITableViewController - Add footer to UITableViewController using storyboard 如何在tableview headerView内部的多行UILabel中添加自动布局约束? - How to add autolayout constraints to a multi-line UILabel inside tableview headerView? 在 tableview 上添加视图(UITableViewController) - Add view over tableview (UITableViewController) 在UITableViewController中的tableview之后添加视图 - Add view behind tableview in UITableViewController 如何使iCarousel与UITableViewController中的headerView一起使用 - How can I make iCarousel work with the headerView in a UITableViewController 如何在Storyboard中将UISegmentedControl插入UITableViewController? - How to insert UISegmentedControl to UITableViewController in Storyboard? 无法在 HeaderView 中添加 UISearchBar(tableview 的 header 视图)? - Not able to add UISearchBar in HeaderView(tableview's header view)? HeaderView和FooterView,Xib,StoryBoard - HeaderView and FooterView, Xib, StoryBoard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM