Hello... friends, I am here with a new blog. Today I am discussing how to search for specific content in a UITableView when it has a lot of data .
When we have a large number of data to display in UITableView, then looking for a particular data is very tedious task. To overcome this problem we can enable a search functionality on the tableview. As I used this in one of my app.
UISearchBar provide a functionality to search the content we look for from the specific view (here it is table view).It is very simple to use UISearchBar in an app. Steps are as follows:
1) Add UISearchBar property in your view controller's interface file and set its delegate
self.searchBar = [[UISearchBar alloc]init];
When we have a large number of data to display in UITableView, then looking for a particular data is very tedious task. To overcome this problem we can enable a search functionality on the tableview. As I used this in one of my app.
UISearchBar provide a functionality to search the content we look for from the specific view (here it is table view).It is very simple to use UISearchBar in an app. Steps are as follows:
1) Add UISearchBar property in your view controller's interface file and set its delegate
self.searchBar = [[UISearchBar alloc]init];
self.searchBar.showsCancelButton = YES;
self.searchBar.delegate = self;
[self.searchBar becomeFirstResponder];
2)I am displaying the search bar in navigation bar as:
self.navigationItem.titleView = self.searchBar;
3) Implement the delegate method of the UISearchBar
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
//hide the searchBar, and reload the tableview to show original data
isSearching = NO;
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
//start the search
NSLog(@"search btn cliked");
[searchBar resignFirstResponder];
[self searhTableList];
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
NSLog(@"search should begin editing");
isSearching = YES;
return YES;
}
Here when user type some text to search and tap the search button, we are searching the table list and then if any matches occur we add the specific row's data to a mutable array and we reload the table view to show the filtered result.
To store the filtered result, declare a mutable array in interface file , here it is filteredArrData,
-(void)searhTableList{
NSString *searchString = self.searchBar.text;
for (NSString *data in self.arr_tableData) {
NSRange range = [data rangeOfString:searchString options:NSCaseInsensitiveSearch];
NSComparisonResult result = [data compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:range];
if (result == NSOrderedSame) {
[self.filteredArrData addObject:data];
}
}
if (self.filteredArrData.count == 0) {
//show an alert, no results found
}else{
//reload the tableview
}
That 's it. Here I am showing how to implement search on table's data.I am not showing how to use same table view to show the search result and original data(full data to show in table view). It's up to you how to use it .
No comments:
Post a Comment