所以我的视图控制器中有一个UITableView
(以及许多不同的东西),我想添加一个搜索选项,所以UISearchBar
可能是最好的方法。< / p>
是否可以在现有UITableView
中添加搜索功能,还是必须重写我的所有视图控制器?
答案 0 :(得分:1)
实施搜索栏委托方法
SearchDisplayController Apple Sample code iOS9
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
// called only once
return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[searchBar setPlaceholder:@"Search your languages"];
// called twice every time
[searchBar setShowsCancelButton:NO animated:YES];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
// called only once
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
// called only once
[searchBar setPlaceholder:@"Select languages"];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length > 0) {
//write your searching code and reload based on search result
//[self searchproduct:searchText];
}
else
{
//arrayLanguage=[searchResultArray mutableCopy];
//reload with original data array when no searching
}
if (searchText.length == 0) {
//when no search reload with original data array
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[_searchBar resignFirstResponder];
}
由于