How do I filter this object array using NSPredicate filteredArrayUsingPredicate / predicateWithBlock?
Class Person is my modal class that has several properties like firstName, lastName etc.
each object of this class is stored in an array objPersonCell. And from my view controller I am displaying fields on UITableViewCell using :
// way similar to this
for(NSMutableArray *obj in self.objPersonCell){
lblFirstName.text = [(Person *)obj personType];
}
Problem : For Search, I want user to type and filter objPersonCell using NSPredicate and immediately reflect the changes by reloading the data on UITableView. Now how do I use NSPredicate.
What I have tried is :
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *searchTerm = [textField.text stringByReplacingCharactersInRange:range withString:string];
[self updateAsPerSearchTerm: searchTerm];
return YES;
}
-(void)updateAsPerSearchTerm:(NSString *)searchTerm
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[cd] %@",searchTerm];
NSArray *sortedArray = [[(Person *)self.objPersonCell] filteredArrayUsingPredicate:predicate];
[self.objPersonCell removeAllObjects];
[self.objPersonCell addObjectsFromArray:sortedArray];
[self.tableView reloadData];
}
The code inside updateAsPerSearchTerm is wrong for passing objPersonCell. How do I set it up. And How predicateWithBlock would better help me out, if can ?