0

I have a table view with a search display controller. When I type a search the log shows me the search is working and filtering properly however the screen only shows "No Results". I'm using Core Data if that makes a difference.

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];
}
else{
    return[[self.fetchedResultsController sections]count];
}
}



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

id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = @"Cell";

if (tableView != self.tableView) {
    NSLog(@"Found searchDisplayController");
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} else {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSLog(@"Found regular table View");
}

    // Configure the cell...
    Instance *instance = [self.fetchedResultsController objectAtIndexPath:indexPath];


    if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    } else {
    cell.textLabel.text = instance.name;
    }

Here are NumberOfRowsInSection and Number:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
     if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];
}
     else{
    return[[self.fetchedResultsController sections]count];
}
}
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
     return [secInfo numberOfObjects];
 }
Isaac
  • 35
  • 1
  • 7
  • where are you updating the contents of the cell ? – Max Sep 07 '13 at 18:48
  • Just below: Instance *instance = [self.fetchedResultsController objectAtIndexPath:indexPath]; //more search data if (tableView == self.searchDisplayController.searchResultsTableView) { cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; } else { cell.textLabel.text = instance.name; } – Isaac Sep 07 '13 at 18:49
  • @Isaac: Can you add that code to your question? That makes it easier to read. - The `numberOfSections` and `numberOfRowsInSection` methods would also be helpful. – Martin R Sep 07 '13 at 19:28
  • Have you looked at the logic that presents the "No results" message and worked back from there to see why it thinks there are no results? – Hot Licks Sep 07 '13 at 19:41
  • (Formatting would be nice.) – Hot Licks Sep 07 '13 at 19:42

2 Answers2

0

Check your implementation of numberOfSectionsInTableView and numberOfRowsInSection.
In the first, you should return 1 if it's the search table view (1 section).
In numberOfRowsInSection you should also check the given tableView argument as you did in numberOfSectionsInTableView and return [searchResults count].

tsafrir
  • 1,711
  • 12
  • 13
0

Shouldn't your implementation be something like :-

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
     if (tableView == self.searchDisplayController.searchResultsTableView) 
     {
       return 1;
     }
     else
     {
       return[[self.fetchedResultsController sections]count];
     }
}
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
      return [searchResults count];
    }
     id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
     return [secInfo numberOfObjects];
 }

If the number of search results is high, then I would suggest using another Fetched Results Controller instead of an array.

Max
  • 4,067
  • 1
  • 18
  • 29
  • thanks for this. I implemented it and inserted log statements to see how the code functioned. Everything was seen and in the log I can see that search is accurately filtering my search but the display still shows No Results. Any other places I should look? – Isaac Sep 08 '13 at 16:40
  • If you are seeing a physical message "No Results", then is there any code which outputs this message ? – Max Sep 08 '13 at 19:46
  • I don't think so. Under CellForRowAtIndexPath I defined what should populate a cell if the tableView == self.searchResultsController.searchResultsTableView. Shouldn't that configure the search result cells? – Isaac Sep 08 '13 at 20:34