如何在没有 Interface Builder 的情况下在 UIViewController 中实现 UISearchDisplayController
我有一个 UIViewController ,它有一个分组的 UITableView 作为属性。我在代码中实例化 UITableView
并且不使用 IB。我想将 UISearchDisplayController
连接到它,但找不到任何示例如何完成此操作。
这就是我所拥有的。 //已在头文件中实现了 UISearchDisplayDelegate
//SearchBar
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
searchBar.barStyle=UIBarStyleBlackTranslucent;
searchBar.showsCancelButton=NO;
searchBar.autocorrectionType=UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
searchBar.delegate=self;
UISearchDisplayController *mySearchDisplayController = [[UISearchDisplayController alloc ]initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController = mySearchDisplayController; //Error here ?? self.searchDisplayController is ReadOnly and can't assign
[self.searchDisplayController setDelegate:self];
[self.searchDisplayController setSearchResultsDataSource:self];
[mySearchDisplayController release];
[myDisplayController release];
这似乎不起作用,UIViewController
的 searchDisplayController 属性似乎是只读的,我无法将 myDisplayController
挂接到它。我真的不确定这是否是正确的方法。
我一直在谷歌上寻找一些关于如何在 UIViewController
中使用 UISearchDisplayController
的提示或教程。我能找到的所有示例都是如何使用 IB 将其实现到 UITableViewController 中,这不是我想要的使用方式。
谁能解释一下我如何才能让它发挥作用?
I have a UIViewController
which has a grouped UITableView
as a property. I instantiate the UITableView
in code and don't use IB. I would like to hook up a UISearchDisplayController
to it but can't find any example how this could be done.
This what I have.
//Have implemented the UISearchDisplayDelegate in the header file
//SearchBar
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
searchBar.barStyle=UIBarStyleBlackTranslucent;
searchBar.showsCancelButton=NO;
searchBar.autocorrectionType=UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
searchBar.delegate=self;
UISearchDisplayController *mySearchDisplayController = [[UISearchDisplayController alloc ]initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController = mySearchDisplayController; //Error here ?? self.searchDisplayController is ReadOnly and can't assign
[self.searchDisplayController setDelegate:self];
[self.searchDisplayController setSearchResultsDataSource:self];
[mySearchDisplayController release];
[myDisplayController release];
This doesn't seem to work, the searchDisplayController propery of the UIViewController
seems to be readonly and I can't hook myDisplayController
onto it. I'm really not sure if this the right way to do it.
I've been looking all around google to find some tip or tutorial on how to use a UISearchDisplayController
in UIViewController
. All the examples I could find was how to implement it into UITableViewController
using IB, which is not the way I want to use it.
Can anyone explain how I could get this working ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我使用的代码。将其放入实例化其自己的 UITableView 的 UIViewController 的 viewDidLoad 中。我认为您正在寻找的部分是将搜索栏添加为表视图的标题视图。
Here's the code that I use. Put this in viewDidLoad of a UIViewController that instantiates it's own UITableView. I think the part you're looking for is to add the search bar as the header view of the table view.
请参阅苹果文档:
See the Apple Docs: