I am trying to implement a searchResultsTableView
that will reload the table based on the user's search. In the numberOfRowsInSection
function, which I know is throwing me the exception, I have the following:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return self.objects.count;
} else {
NSLog(@"The count of self.searchresults is %i", self.searchResults.count); //Shows as non-zero
return self.searchResults.count;
}
}
What I am confused is that I have set a breakpoint right before the return
statement, and I know I am getting non-0 results based on the NSLog. Immediately after the returning the non-0 number, the exception is thrown, and I get the exception "index 0 beyond bounds for empty array"
What am I doing wrong?
I also made sure to reload the table only manually, so in shouldReloadTableForSearchString
, I have the following method:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
if (searchString.length > 3){
[self.searchResults removeAllObjects];
PFQuery *query = [PFUser query];
[query whereKey:@"username" containsString:searchString];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(@"There is an error");
} else {
[self.searchResults addObjectsFromArray:objects];
NSLog(@"self.resultResults count %i", self.searchResults.count);
NSLog(@"The objects are %@", self.searchResults);
[self.searchDisplayController.searchResultsTableView reloadData];
}
}];
}
return NO;
}
Thanks!