UISearchDisplayController,UITableView 的引用是什么?

发布于 2024-12-13 21:23:59 字数 126 浏览 0 评论 0原文

如果我有多个 UITableView 和一个带有 UISearchDisplayController 的 UISearchBar,在 UITableView 委托和数据源方法中,如何获取对用于搜索的 UITableView 的引用?谢谢。

If I have multiple UITableViews, and a UISearchBar with UISearchDisplayController, in the UITableView delegate and datasource methods, how do I get a reference to the UITableView for the Search? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

感情旳空白 2024-12-20 21:23:59

通过连接到 NIB 文件的插座或以编程方式创建 searchBar 时获取对 UISearchDisplayController 的引用。获得引用后,在所有数据源和委托方法中,您只需检查正在传递的 tableView 并采取适当的操作。例如:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (aTableView==self.searchDisplayController.searchResultsTableView) {
         // do something with the search data source
         self.searchDisplayController.active=NO;
    }else{
         // do something with the regular data Source
    }

    // present a view or other response to the selected row
}

以下是您如何创建搜索栏并获取对 UISearchDisplayController 的引用

-(UISearchBar *)searchBar{
    // Create a search bar
    if (searchBar==nil){
        UISearchBar *aSearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)] autorelease];
        aSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
        aSearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
        aSearchBar.keyboardType = UIKeyboardTypeAlphabet;
        aSearchBar.barStyle=UIBarStyleBlack;
        self.searchBar=aSearchBar;

        // Create the search display controller
        UISearchDisplayController *aSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self] ;
        aSearchDisplayController.searchResultsDataSource = self;
        aSearchDisplayController.searchResultsDelegate = self;
        aSearchDisplayController.delegate=self;
        self.searchDisplayController=aSearchDisplayController;
        [aSearchDisplayController release];
    }
    return searchBar;

然后将其添加到您的 tableView 中,如下所示:

self.tableView.tableHeaderView = self.searchBar;

祝您好运

get a reference to your UISearchDisplayController either via an outlet connected to your NIB file or when you create the searchBar programmatically. Once you have the reference, in all of your datasource and delegate methods you can just check to see which tableView is being passed and act appropriately. For example:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (aTableView==self.searchDisplayController.searchResultsTableView) {
         // do something with the search data source
         self.searchDisplayController.active=NO;
    }else{
         // do something with the regular data Source
    }

    // present a view or other response to the selected row
}

here is how you might create the search bar and get the reference to the UISearchDisplayController

-(UISearchBar *)searchBar{
    // Create a search bar
    if (searchBar==nil){
        UISearchBar *aSearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)] autorelease];
        aSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
        aSearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
        aSearchBar.keyboardType = UIKeyboardTypeAlphabet;
        aSearchBar.barStyle=UIBarStyleBlack;
        self.searchBar=aSearchBar;

        // Create the search display controller
        UISearchDisplayController *aSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self] ;
        aSearchDisplayController.searchResultsDataSource = self;
        aSearchDisplayController.searchResultsDelegate = self;
        aSearchDisplayController.delegate=self;
        self.searchDisplayController=aSearchDisplayController;
        [aSearchDisplayController release];
    }
    return searchBar;

}

then add it to your tableView like this:

self.tableView.tableHeaderView = self.searchBar;

Good luck

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