【问题标题】:Add UISearchBar programmatically to a programmatically added UITableView in a normal ViewController以编程方式将 UISearchBar 添加到普通 ViewController 中以编程方式添加的 UITableView
【发布时间】:2013-06-11 22:45:31
【问题描述】:

我有一个非常特殊的要求。由于情况不同,没有教程或已经提出的问题对我有用。它很特别,因为我想在不重新编程所有代码的情况下增强现有应用程序。

我想以编程方式添加 UISearchbar。 搜索栏应该添加到 UITableView 中,该 UITableView 也是以编程方式添加的。

这一切都发生在一个普通的 UIViewController 中。

我当前的方法显示搜索栏,但它不在我的 UITableView 中搜索。 根本没有任何反应。

我做错了什么?

myTableView    =   [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;

myTableView.tableHeaderView = searchBar;

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

myTableView.dataSource      =   self;
myTableView.delegate        =   self;

[self.view addSubview:myTableView];

【问题讨论】:

    标签: objective-c uitableview uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    您的对象是否实现了UISearchDisplayDelegate 中的委托方法?

    换句话说,您必须定义在搜索栏中输入文本时会发生什么,它不会自动为您发生。

    【讨论】:

      【解决方案2】:

      除了确保你的 ViewController 符合 UISearchBarDelegate 之外,像这样:

      @interface YourViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
      

      你应该实现以下方法

      - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
      {
          [searchBar resignFirstResponder];
          [self.view endEditing:YES];
          // do search here
      } 
      

      【讨论】:

        猜你喜欢
        • 2011-10-20
        • 1970-01-01
        • 1970-01-01
        • 2019-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 2017-11-24
        相关资源
        最近更新 更多