如何在没有 Interface Builder 的情况下在 UIViewController 中实现 UISearchDisplayController

发布于 2024-11-05 19:12:13 字数 1371 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴梦一场 2024-11-12 19:12:13

这是我使用的代码。将其放入实例化其自己的 UITableView 的 UIViewController 的 viewDidLoad 中。我认为您正在寻找的部分是将搜索栏添加为表视图的标题视图。

UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.
theSearchBar.delegate = self;
if ( !searchBarPlaceHolder ) {
    searchBarPlaceHolder = @"What are you looking for?";
}
theSearchBar.placeholder = searchBarPlaceHolder;
theSearchBar.showsCancelButton = YES;


self.theTableView.tableHeaderView = theSearchBar;

UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
                                        initWithSearchBar:theSearchBar
                                        contentsController:self ];
self.searchController = searchCon;
[searchCon release];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;

[searchController setActive:YES animated:YES];
[theSearchBar becomeFirstResponder];

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.

UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.
theSearchBar.delegate = self;
if ( !searchBarPlaceHolder ) {
    searchBarPlaceHolder = @"What are you looking for?";
}
theSearchBar.placeholder = searchBarPlaceHolder;
theSearchBar.showsCancelButton = YES;


self.theTableView.tableHeaderView = theSearchBar;

UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
                                        initWithSearchBar:theSearchBar
                                        contentsController:self ];
self.searchController = searchCon;
[searchCon release];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;

[searchController setActive:YES animated:YES];
[theSearchBar becomeFirstResponder];
后知后觉 2024-11-12 19:12:13

请参阅苹果文档:

@property(非原子,只读,保留)UISearchDisplayController *searchDisplayController

讨论:该属性反映了
您在 Interface Builder 中设置的 searchDisplayController 出口。如果
您以编程方式创建搜索显示控制器,这
属性由搜索显示控制器自动设置
它已初始化。

See the Apple Docs:

@property(nonatomic, readonly, retain) UISearchDisplayController *searchDisplayController

Discussion: This property reflects the value of the
searchDisplayController outlet that you set in Interface Builder. If
you create your search display controller programmatically, this
property is set automatically by the search display controller when
it is initialized
.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文