2011-05-24 63 views
2

如何在UITableView上添加搜索欄?只需要搜索欄就足夠了還是會選擇搜索欄和搜索顯示?在UITableView上添加搜索欄

我想用搜索找到的項目重繪tableview。

回答

8

UISearchDisplayController是專門爲解決您的問題而設計的。您應該能夠通過閱讀文檔來了解如何使用它。

當然你可以自己處理UISearchBar(或者如果你想建立自己的搜索欄,甚至可以使用UITextField)。

下面是一些代碼,讓你開始:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

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

    self.tableView.tableHeaderView = searchBar; 

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

幾乎所有有剩下來要做的就是執行代表。如果您需要任何幫助,請告訴我,但我可以建議您針對您遇到的每個問題提出一個新問題。當然,如果你在這裏留言,我會看看它。