Reputation: 857
I've created a SearchBar thats suppose to filter a UITableView I have setup. I'm using Parse.com to pull this array down into my app.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self Query];
//[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"SearchCell" bundle:nil] forCellReuseIdentifier:@"SearchData"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)Query {
PFQuery *Q = [PFQuery queryWithClassName:@"_User"];
[Q selectKeys:@[@"username"]];
[Q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"load Array);
SearhArray = [[NSArray alloc] initWithArray:objects];
}
else{
NSLog(@"Array No Good");
}
}];
}
Then I attempt to filter the array Using these lines of code
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
self.searchTerm = searchText;
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.mySearchBar resignFirstResponder];
//reset to the original array and reload table
self.filteredCities = SearhArray;
[self.myTableView reloadData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self.mySearchBar resignFirstResponder];
if ([self.searchTerm isEqualToString:@""]) {
// Nothing to do here - Reset to the original array and reload table
self.filteredCities = SearhArray;
} else {
// Filter the array based on the term searched for using a predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.title contains[c] %@",self.searchTerm];
self.filteredCities = [NSArray arrayWithArray:[SearhArray filteredArrayUsingPredicate:predicate]];
}
[self.myTableView reloadData];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
self.filteredCities = SearhArray;
[self.myTableView reloadData];
}
But it just keeps crashing. I've debugged the problem down to this line
self.filteredCities = [NSArray arrayWithArray:[SearhArray filteredArrayUsingPredicate:predicate]];
The error I get is:
`2015-03-31 19:31:19.783 Pondu[59307:5149459] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Key "words" has no data. Call fetchIfNeeded before getting its value.'
*** First throw call stack:
(0x18724a59c 0x1979a00e4 0x18724a4dc 0x1000c34bc 0x1880350f0 0x18807b934 0x18807b394 0x18807a084 0x188079e58 0x1000b31b0 0x18be207ac 0x18ba30d34 0x18ba19e48 0x18bbf91dc 0x18bbc3f04 0x18bd80890 0x18bd80540 0x18ba24950 0x18811ddf0 0x1872029ec 0x187201c90 0x1871ffd40 0x18712d0a4 0x1902cf5a4 0x18ba623c0 0x1000b1f64 0x19800ea08)
Libc++abi.dylib: terminating with uncaught exception of type NSException`
Upvotes: 0
Views: 492
Reputation: 898
self.searchTerm seems like NSString but what you are put there is you are supposing that value as NSDictionary. You need to use something like
@"SELF MATCHES %@"
, NOT
@"SELF.title MATCHES %@"
Upvotes: 1