将子视图添加到不滚动的tableview [英] Add subview to tableview which doesn't scroll

查看:100
本文介绍了将子视图添加到不滚动的tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UITableViewController并希望添加搜索栏。

I'm using a UITableViewController and want to add a searchbar.

我的选项是 [self.tableview addSubview:searchBar] self.tableview.tableHeaderView = searchBar
两个选项都会沿着tableview的其余部分滚动搜索栏,我理解。但有没有办法锁定元素,而不是使用UIViewController或更改滚动框架原点?
我正在考虑一种超越当前视图层次结构并在其上添加子视图的方法。

My options are [self.tableview addSubview:searchBar] or self.tableview.tableHeaderView = searchBar Both options will scroll the searchbar along the rest of the tableview, which I understand. But is there a way to lock elements up, instead of using a UIViewController or changing the frame origin on scroll? I'm thinking of a way to get above the current view hierarchy and add a subview onto that.

我尝试了相反的方法,添加一个视图到superview并把它带到前面

I tried the opposite approach, to add a view to the superview and bring that to front

[[self.tableView superview] addSubview:searchBar];
[[self.tableView superview] bringSubviewToFront:searchBar];

}

但它不起作用。

推荐答案

UITableViewControllers UIViewControllers 主视图属性为 UITableView 。因此,这个 UITableView 会占用所有屏幕。

UITableViewControllers are UIViewControllers that have their main view property being an UITableView. Thus this UITableView do takes all the screen.

您正在寻找的方法是使用标准 UITableViewController 其视图是标准 UIView ,并添加 UISearchBar UITableView 作为IB的子视图。

The approach you are looking for is using a standard UITableViewController whose view is a standard UIView, and add your UISearchBar and the UITableView as a subview to it in IB.

要做到这一点,只需:


  • 更改视图控制器的类,改为提及它符合tableview相关协议:更改 @interface UITableViewController @interface UIViewController< UITableViewDelegate,UITableViewDataSource>

  • 添加 @property(非原子,保留)IBOutlet UITableView * tableView 属性到头文件,以及实现中关联的 @synthesize tableView; 语句。不要忘记在<$ c $中将此属性设置回 nil self.tableView = nil ) c> dealloc 用于良好的内存管理

  • 将InterfaceBuilder中的 UITableView 实例链接到这个新创建的IBOutlet。

  • Change the class of your view controller and instead mention that it conforms to the tableview-associated protocols : change @interface UITableViewController to @interface UIViewController <UITableViewDelegate, UITableViewDataSource>
  • Add a @property(nonatomic, retain) IBOutlet UITableView* tableView property to your header file, and the associated @synthesize tableView; statement in the implementation. Don't forget to set this property back to nil (self.tableView = nil) in your dealloc for good memory managment
  • Link your UITableView instance in InterfaceBuilder to this newly created IBOutlet.

因此你仍然有一个UITableView,但这不是占用你所有屏幕的主视图;相反,你可以像你想要的那样布局你的tableView,并使它只占用你的屏幕的一部分,并将UISearchBar放在它上面。

Thus you still have a UITableView but this won't be the main view that takes all your screen; instead you can layout your tableView like you want and make it take only a part of your screen, and position the UISearchBar above it.

另一种方法是使用 UISearchDisplayController 来管理您的UISearchBar。

Another approach would be to use the UISearchDisplayController to manage your UISearchBar.

这篇关于将子视图添加到不滚动的tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆