0

I have a core data database and each row is shown in a table.

I have a search controller to enable the user to search by first or last name.

When i get results from 'filteredArrayUsingPredicate' i reload the search controller table and all works fine. The table shows the filtered results.

When it comes to select a result, the results array is empty and subsequently crashes due to [array atIndex:index path.row] out of bounds. The array is empty making me think it has been released.

This code works perfectly on another view. i have tried re writing and clean and build and still happening- any ideas?

Below is my code

@property(nonatomic,retain) NSArray* cachedSearchResults;

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{


    NSPredicate * predicate =  [NSPredicate predicateWithFormat:@"first_name contains[cd] %@ || last_name contains[cd] %@",searchText, searchText];
    self.cachedSearchResults = [[self.cachedResults filteredArrayUsingPredicate:predicate]retain];
;
    [searchController.searchResultsTableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

PupilManagedObject* selected = nil;
if(tableView == table){
    selected = [cachedResults objectAtIndex:indexPath.row];
}else{
    selected = [self.cachedSearchResults objectAtIndex:indexPath.row]; //array is empty at this point
}
}
Dan
  • 1,447
  • 2
  • 14
  • 30
  • Logging the array at tableView:numberOfRowsInSection and cellForRowAtIndexPath shows a correctly populated array – Dan Apr 12 '12 at 18:36

2 Answers2

0

I found the issue was the search controller was dismissed before i made use of the data in the array. odd but changing the order of my code worked

Dan
  • 1,447
  • 2
  • 14
  • 30
-1

This code snippit is lacking important information. From what I see, it sounds like you're not correctly computing tableView:numberOfRowsInSection:. Verify that it is providing the correct number of rows for your given scenario.

Sean Freitag
  • 930
  • 1
  • 6
  • 15
  • thanks - it is. Logging the array at tableView:numberOfRowsInSection - cellForRowAtIndexPath shows a populated array. It seems to be released between that and selection – Dan Apr 12 '12 at 18:32
  • Is the array actually empty in the tableView:didSelectRowAtIndexPath:, or is it dealloc'd? I'm confused as to where the variable "table" comes from, and why you need to switch between them. Have you logged the index path coming into that function to make sure it's what you expect? – Sean Freitag Apr 12 '12 at 21:45
  • Thanks for your response. When printing the array it is not nil, it is empty. The comparison is to check if delegate was called from search controller or table. The index path is correct, the issue is the array is being emptied for where for some reason. I have used the exact same code at least 5 times in the same app with no issues. It's very odd.. – Dan Apr 13 '12 at 06:31
  • What more code do you want to help? There noting really missing? – Dan Apr 13 '12 at 21:10
  • Obviously there is, because there is nothing that would suggest a release of objects in your array in the attached code. If you want help, you'll have to put more of the source online, perhaps using GitHub. – Sean Freitag Apr 16 '12 at 15:17