Reputation: 2796
I am using NSPredicate
for search record from the tableView
.
Below is my code which I have implemented.
-(void)updateSearchArray:(NSString *)searchText {
if(searchText.length==0) {
isFilter=NO;
} else {
isFilter=YES;
filteredUsers = [[NSMutableArray alloc]init];
NSPredicate *resultPredicate;
resultPredicate = [NSPredicate predicateWithFormat:@"displayname contains[c] %@", searchText];
NSLog(@"%@",[tableData valueForKey:@"displayname"]);
filteredUsers = [[tableData valueForKey:@"displayname"] filteredArrayUsingPredicate:resultPredicate];
[self.tblMemberList reloadData];
}
}
My tableData
:
Printing description of self->tableData:
<__NSArrayI 0x28116ba60>(
{
displayname = Mihir;
email = "[email protected]";
uuid = "user-97ae136";
},
{
displayname = OzaMihir;
email = "[email protected]";
uuid = "user-0c97f16";
}
)
My app crashes when I use above code.
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key displayname.'
Thanks in advance.
Upvotes: 0
Views: 417
Reputation: 714
Try the below code.
filteredUsers = [temp filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"displayname CONTAINS[c] %@", searchText]];
[self.tblMemberList reloadData];
And you are passing an array of displayname
in your code to filter it. If you want to perform the operation only on array of displayname
array. then you can do it like below.
//It will perform operation/search name only on an array of `displayname`
filteredUsers = [[tableData valueForKey:@"displayname"] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchText]];
[self.tblMemberList reloadData];
I hope it will helpful to you.
Upvotes: 1
Reputation: 82759
your tableData contains array of dictionary, so in here search occur direct in the array not a string, so use as like
isFilter=YES;
filteredUsers = [[NSMutableArray alloc]init];
filteredUsers = [tableData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(displayname contains[c] %@)", searchText]];
[self.tblMemberList reloadData];
instead of
NSPredicate *resultPredicate;
resultPredicate = [NSPredicate predicateWithFormat:@"displayname contains[c] %@", searchText];
NSLog(@"%@",[tableData valueForKey:@"displayname"]);
filteredUsers = [[tableData valueForKey:@"displayname"] filteredArrayUsingPredicate:resultPredicate];
Upvotes: 1