Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching a table view with UISearchBar

Tags:

xcode

ios

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?

like image 712
user2014474 Avatar asked Jan 15 '23 05:01

user2014474


2 Answers

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]; 
    }
like image 91
Rahul Wakade Avatar answered Jan 16 '23 20:01

Rahul Wakade


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;
}
like image 34
Bot Avatar answered Jan 16 '23 18:01

Bot