Shelby
Shelby

Reputation: 81

Searchbar/filtering process using serachbar in IOS objective c

Can anyone please help me find out why am i not able to fetch filtered array when searchbar delegate method is called while entering text in searchbar

My textDidChange method for toolbar

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
     if(searchText.length == 0){
        isFiltered = NO;
     }else{
         isFiltered = YES;
         [filterdArray removeAllObjects];
         for(int i = 0; i < [productArray count]; i++){
              NSRange textRange;
              textRange =[[[[productArray objectAtIndex:i] objectForKey:@"senior_name"] lowercaseString] rangeOfString:[searchText lowercaseString]];

              if(textRange.location != NSNotFound){
                  [filterdArray addObject:[productArray objectAtIndex:i]];
                  NSLog(@"filterdArrayyyyyyyy:%@",filterdArray);
              }
         }
     }

     [self.residentListTableView reloadData];   
}

this is my productArray,

(
    {
    id = 369;
    "room_no" = 101;
    "senior_name" = Tim;
},
    {
    id = 388;
    "room_no" = "<null>";
    "senior_name" = res444;
},
    {
    id = 382;
    "room_no" = "<null>";
    "senior_name" = tt1234;
},......

filterarray is returning null,

I want to filter tableview based on "senior_name"

Thank you in advance,

Upvotes: 0

Views: 828

Answers (3)

M Faheem Rajput
M Faheem Rajput

Reputation: 778

for new viewers of this Question @shelby didn't init array in viewDidLoad he just declare it.

Thumb rule are same

Declaration NSMutableArray *filterdArray ; // it is just declaration

Initialisation [[NSMutableArray alloc]init] OR [NSMutableArray new]

Upvotes: 0

Pooja Gupta
Pooja Gupta

Reputation: 793

Add following code in searchBar delegate method-

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

 NSString *textToSearch = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]; // Its better to remove whitespaces & newline characters

 isFiltered = textToSearch.length ? YES : NO;

NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"self.senior_name MATCHES[cd] %@",textToSearch]; // This predicate will search for exact case-insensitive match. You can change your predicate also. 
filteredArray = [productArray filteredArrayUsingPredicate: searchPredicate];

[filteredArray removeAllObjects];
if (shouldShowSearchResults && filteredArray.count == 0) {        
   // Show a view like no search results found        
}
else {
    [self.residentListTableView reloadData];
  }    
 }

In your tableView delegate methods add the following-

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (isFiltered ? filteredArray.count : productArray.count);
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:CELL_ID];

if(isFiltered){
  // Load data from filteredArray
}else{
// Load data from productArray
    }
  return cell;
}

Upvotes: 0

Nishant Bhindi
Nishant Bhindi

Reputation: 2252

You can try below code instead to get the appropriate result.

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
 if(searchText.length == 0){
    isFiltered = NO;
 }else{
     isFiltered = YES;
     [filterdArray removeAllObjects];
     for(int i = 0; i < [productArray count]; i++){
        if([[[[productArray objectAtIndex:i] objectForKey:@"senior_name"] lowercaseString] rangeOfString:[searchText lowercaseString]].length>0){
            [filterdArray addObject:[productArray objectAtIndex:i]];
            NSLog(@"filterdArrayyyyyyyy:%@",filterdArray);
        }
    }
 }

 [self.residentListTableView reloadData];   
 }

Upvotes: 2

Related Questions