user7018875
user7018875

Reputation: 160

search bar method for array of dictionarys in ios using NSPredicate method

I am new in IOS and I am creating a search bar for UITableView. It is working fine when I use an array directly, but when I have an array with a dictionary it's not working.

I have an array of dictionaries:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableDictionary    *person1 = [NSMutableDictionary dictionaryWithDictionary:@{
                            @"Name" : @"john",
                            @"Age" : @"10"
                            }];
    NSMutableDictionary  *person2  = [NSMutableDictionary dictionaryWithDictionary:@{
                        @"Name" : @"piter",
                        @"Age" : @"22"
                                   }];
    NSMutableDictionary *person3  = [NSMutableDictionary dictionaryWithDictionary:@{
                        @"Name" : @"rams",
                        @"Age" : @"23"                                                                          
                                      }];

    self.person = [[NSMutableArray alloc]initWithObjects:person1,person2,person3 ,nil];
}

I have print those dictionary name on UITableView.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *simpleTableIdentifier = @"myCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.textLabel.text = [[self.personobjectAtIndex:indexPath.row]objectForKey:@"Name"];
    return cell;
}

It will give me list of names in UITableView. I used a search bar and I want to search for specific words and have them show on my UITableView, but its not working using this code:

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

    if([searchText length] == 0){
        self.isfiltered = NO;
    }
    else{
        self.isfiltered = YES;
        NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",searchText];
        filteredList = [self.person filteredArrayUsingPredicate:resultPredicate];
    }

    [self.mytable reloadData];
}

Upvotes: 1

Views: 110

Answers (2)

Sumayya Ansari
Sumayya Ansari

Reputation: 79

you need to change 'SELF' -> 'Name'.

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

        if([searchText length] == 0){
            self.isfiltered = NO;
        }
        else{
            self.isfiltered = YES;
            NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"Name contains[cd] %@",searchText];
            filteredList = [self.person filteredArrayUsingPredicate:resultPredicate];
        }

        [self.mytable reloadData];
    }

Then change in your table array.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

   if (self.isfiltered)
    return filteredList.count;
   else
    return  self.person.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"myCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; }
if (self.isfiltered)
cell.textLabel.text = [[self.isfiltered objectAtIndex:indexPath.row]objectForKey:@"Name"];

else
 cell.textLabel.text = [[self.person objectAtIndex:indexPath.row]objectForKey:@"Name"];


   return cell;
}

Upvotes: 0

KKRocks
KKRocks

Reputation: 8322

You need to change your data source array while searching .

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return   self.isfiltered ? filteredList.count : self.person.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"myCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; }
cell.textLabel.text = [[ self.isfiltered ? filteredList : self.person objectAtIndex:indexPath.row]objectForKey:@"Name"]; }

   return cell;
}

Upvotes: 1

Related Questions