0

I have an UISearchBar and UISearchDisplayController implemented correctly to filter the cells in a UITableView.

Sometimes it works fine and filters the cells according to the entered search term. But sometimes - without changing a line of code - it doesn't filter and shows all cells.

In the tableView delegates I notice that the IF statement below would be FALSE and thus display all contacts as search results in searchResultsTableView.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [_searchResultContacts count];
    }
    else
    {
        return [_allContacts count];
    }
}

Why is that?

(Don't answer, I will post the answer below!)

Manuel
  • 14,274
  • 6
  • 57
  • 130

1 Answers1

0

The reason was that I accidentally added multiple UISearchDisplayControllers in the same view in story board.

Because of overlapping I did not notice it until I saw it in the document outline window of Xcode.

enter image description here

After removing all but one search display controller, it worked fine. Make sure the outlet connections are properly set.

Took me some time to find out, so I hope this is of help to someone.

Manuel
  • 14,274
  • 6
  • 57
  • 130