Ancinek Ancinek - 4 months ago 15
Objective-C Question

UISearchDisplay in existing UITableView

So I have a

UITableView
in my view controller (as well as many different things) and I wanted to add a search option, so
UISearchBar
will be probably the best way to do it.

Is it possible to add a search function in my existing
UITableView
or do I have to rewrite all my view controller?

Answer

Implement searchbar delegate method

UISearchBarDelegate

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];
}

Thanks