I am creating an application with a table view, which has a large amount of data in it. Because of this, it is necessary for me to use a search field.
Does anyone have any idea how to create a开发者_StackOverflow search-option in a tableview?
Use the UISearchBar property, declare it using
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,30)];
and add it to your UIViewController's view as a subview.
After that, use the searchbar delegate methods in your view controller:
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
Check out this tutorial to get a hang of the search bar and its delegates!
EDIT: This method isn't about using a searchbar in tableview itself, but above it. That means you have to put tableView as a subview of a UIViewController, and the searchBar as a subview of the same UIViewController as well!
精彩评论