0

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!

daspianist
  • 5,336
  • 8
  • 50
  • 94
  • What is your code that is accessing the array? Most likely in `RowForIndexPath:` – Evan Jan 29 '14 at 05:29
  • Add an exception breakpoint. Breakpoints tab, plus sign at the bottom. – danh Jan 29 '14 at 05:30
  • I added an exception breakpoint for all exceptions, and also a breakpoint right at `tableView cellForRowAtIndexPath:`. I found out that `cellForRowAtIndexPath` is actually not being called at all. Is there any tableview method that gets called AFTER `numberOfRowsInSection`? – daspianist Jan 29 '14 at 06:22
  • `cellForRowAtIndexPath` is called when a cell is to be presented. Check other delegate methods like `tableView:heightForRowAtIndexPath:` etc which can be accessing your datasource array. The Out of bounds error is because you might be trying to fetch at some index out of an array's count. Certainly that's not the case in your numberOfRowsInSection method. – Zen Jan 29 '14 at 09:20

3 Answers3

1

It seems to be your application is not crashing in

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

Check where ever you are calling objectAtIndex: method and see if you are making a call on invalid index.

Naga Mallesh Maddali
  • 1,005
  • 10
  • 19
0

Try this,

Change your searchDisplayController: to

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    if (searchString.length > 3){

        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 setArray:objects];
                NSLog(@"self.resultResults count %i", self.searchResults.count);
                NSLog(@"The objects are %@", self.searchResults);
               [self.searchDisplayController.searchResultsTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
            }
       }];

    }
    return NO;
}
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30
  • Thanks for suggesting this. Unfortunately even by getting rid of the `removeAllObjects` method and setting the Array to be the objects from the research, I still get the exception. The exception is thrown when the array has >0 items. – daspianist Jan 29 '14 at 06:33
  • try reloading table in main thread, `[self.searchDisplayController.searchResultsTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];` – Akhilrajtr Jan 29 '14 at 07:37
0
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    if (searchString.length > 3){
         if(self.searchResults){
            [self.searchResults release];
             self.searchResults=nil;
         }

       // [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];
                self.searchResults=[[NSMutableArray alloc] initWithArray:objects];

                NSLog(@"self.resultResults count %i", self.searchResults.count);
                NSLog(@"The objects are %@", self.searchResults);
                [self.searchDisplayController.searchResultsTableView reloadData];
            }
        }];

    }
    return NO;
}

Instead of [self.searchResults removeAllObjects]; use

if(self.searchResults){
            [self.searchResults release];
             self.searchResults=nil;
         }

and replace:

 [self.searchResults addObjectsFromArray:objects]; 

with

 self.searchResults=[[NSMutableArray alloc] initWithArray:objects];
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
  • I am afraid this didn't work. When self.searchResults was returning 0 it was working fine.. but when its non-zero I get the exception. – daspianist Jan 29 '14 at 06:32