Searching a table view with UISearchBar

后端 未结 2 1924
灰色年华
灰色年华 2021-01-13 17:48

I am using this code to search through a UItableView:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {



if(searchText.lengt         


        
相关标签:
2条回答
  • 2021-01-13 18:23

    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;
    }
    
    0 讨论(0)
  • 2021-01-13 18:25

    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]; 
        }
    
    0 讨论(0)
提交回复
热议问题