1

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

我想以编程方式添加一个 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];
4

2 回答 2

2

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

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

于 2013-06-11T23:22:45.270 回答
1

除了确保您的 ViewController 符合 UISearchBarDelegate 之外,如下所示:

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

你应该实现以下方法

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [self.view endEditing:YES];
    // do search here
} 
于 2015-10-06T07:13:44.533 回答