以编程方式将UISearchBar添加到UITableView [英] Adding UISearchBar Programmatically to UITableView

查看:117
本文介绍了以编程方式将UISearchBar添加到UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加 UISearchBar (及其相应的搜索逻辑),但是有一个问题:我使用的是 UITableViewController 子类自动生成 UITableView 而不是单独的 nib 文件,并根据表格以编程方式操作所有内容查看。

I am attempting to add a UISearchBar (with its corresponding search logic) but there is a problem: I am using the UITableViewController subclass's automatically generated UITableView instead of a separate nib file, and manipulating everything programmatically in terms of the table view.

在Interface Builder中,可以选择将搜索栏搜索显示控制器添加到笔尖。有没有办法以编程方式完成相同的任务或是否有利于放弃默认的 UITableView 并转移到自定义 UITableView nib文件,这样我就可以轻松添加 UISearchbar

In Interface Builder, there is an option to add a Search Bar and Search Display Controller to a nib. Is there a way to accomplish this same task programmatically or is it in my favor to abandon the default UITableView and transfer to a custom UITableView nib file so that I can easily add the UISearchbar?

另外,我试过测试一下通过我的 viewDidLoad UITableView 标题(我希望它去的地方) >实现,但它显示在表部分标题后面,因此是不可见的,除非向下滚动表以显示否则将是空白区域。知道这是怎么回事?

Also, I have tried testing out just adding a Search Bar to my UITableView header (which is where I want it to go) via the following code in my viewDidLoad implementation, but it shows up behind table section header and hence is invisible unless the table is scrolled down to show what would otherwise be white space. Any idea what's up with this?

UISearchBar *testbar = [[UISearchBar alloc] init];

[[self tableView] setTableHeaderView:testbar];


推荐答案

设置UISearchBar的框架:

Set the frame for the UISearchBar:

// Change the position according to your requirements
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 70, 320, 44)];

如果您的视图是 UITableViewController的子类 ,将其更改为 UIViewController

If your view is a subclass of UITableViewController, change it to UIViewController.


  • 在您的nib文件中,创建在
    视图下查看并拖动tableView,并将该视图的引用类更改为
    UIViewController

创建一个 IBOutlet UITableView * myTableView 并将它连接到你的nib文件
。你只需要改变你的VC文件,例如self到 [self.myTableView reloadData];

Create an IBOutlet of UITableView * myTableView and connect it to your nib file. you just need to change in your VC file for example self to [self.myTableView reloadData];

现在你可以在笔尖本身中调整 tableView 和搜索栏。

Now you can adjust the tableView and search bar in the nib itself.

UISearchDisplay controller有自己的 UITableView ,它显示搜索由另一个视图控制器管理的数据的结果。这里 searchDisplaycontroller 结合了搜索栏和tableview,以在tableview中显示结果数据,因此不需要单独的tableview。

UISearchDisplay controller has its own UITableView that displays the results of a search of data managed by another view controller. Here the searchDisplaycontroller combines the search bar and tableview to show the result data in the tableview so it won't require a separate tableview.

这篇关于以编程方式将UISearchBar添加到UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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