I am using this code to search through a UItableView:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if(searchText.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = TRUE;
if (filteredTableData == nil)
filteredTableData = [[NSMutableArray alloc] init];
else
[filteredTableData removeAllObjects];
for (NSString* string in self.itemArray)
{
NSRange nameRange = [string rangeOfString:searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if(nameRange.location != NSNotFound)
{
[filteredTableData addObject:string];
}
}
}
[tableView reloadData];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}
Everything works fine, but if I press the cancel button which appears when I start editing, my list doesn't come back, but the search results remain. To show back the list, I have to begin typing, even a single character in the searchbar and then delete it or press the "x" to see all the list. Is there a way to block the cancel button? Or to show the list when it is pressed?
Implement - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar delegate method of UISearchBar. In that add all objects(initial objects in table) in filteredTableData array and reload table.
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[filteredTableData removeAllObjects];
[filteredTableData addObjectsFromArray:self.itemArray];
[tableView reloadData];
}
Also you don't need to maintain flag isFiltered if you are using it for choosing data source array for reloading table(in table view data source methods). e.g.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFiltered)
return filteredTableData.count
else
return self.itemArray.count
}
If you maintain data in filteredTableData array all time, you don't need to do this. Your method will look like-
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return filteredTableData.count
}
Initially in init or viewDidLoad method of controller add all objects in filteredTableData and use only this array for reloading table.
Hence your method will look like-
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0)
{
[filteredTableData removeAllObjects];
[filteredTableData addObjectsFromArray:self.itemArray];
}
else
{
[filteredTableData removeAllObjects];
for (NSString* string in self.itemArray)
{
NSRange nameRange = [string rangeOfString:searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if(nameRange.location != NSNotFound)
{
[filteredTableData addObject:string];
}
}
}
[tableView reloadData];
}
I don't use that method but use the following and it works perfectly
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",searchString];
self.filteredCustomers = [[self.customers filteredArrayUsingPredicate:predicate] mutableCopy];
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.searchDisplayController setActive:NO animated:NO];
self.filteredCustomers = nil;
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomerCell";
NSDictionary *customerObject;
CustomerListCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (self.searchDisplayController.active) {
customerObject = [[NSDictionary alloc] initWithDictionary:[self.filteredCustomers objectAtIndex:indexPath.row]];
} else {
customerObject = [[NSDictionary alloc] initWithDictionary:[[self.customerData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
}
cell.textLabel.text = [customerObject objectForKey:@"name"];
cell.customerName = [customerObject objectForKey:@"name"];
cell.customerId = [customerObject objectForKey:@"customer_id"];
return cell;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With