Search code examples
ios7xcode5uisearchdisplaycontroller

Returning to UISearchDisplayController after selecting a row in UISearchDisplayController results in strange behavior


I am creating an app for iOS 7 in XCode 5.1.1 that uses a table view controller which has the results filtered by distance from user (default behavior right now). I added search capability, which works fine except it will not use my custom UITableViewCell (not the issue I need help with, but I mention it on the off chance that it might be relevant to my current problem).

Everything works fine when I select a row in the normal table view and segue to the details screen and back. When I search and select a row in the search display controller, it segues to the correct detail screen, but when I return to the search results it adds a row at the bottom that would normally appear in the regular table view. Every time I select a row in the search results, segue, and then return to the search results screen, another row is added at the bottom. These rows are selectable, but do not perform segues. Here are some images to help clarify the problem:

regular tableview rows

regular tableview rows


detail view after selecting row

detail view after selecting row


the tableview after returning from detail view

the tableview after returning from detail view


search bar becomes active

search bar becomes active


search results return after searching for "the" but not using custom tableviewcell

search results return after searching for "the" but not using custom tableviewcell


correct detail view displays after selecting a search result

correct detail view displays after selecting a search result


returning from detail view to the searchdisplaycontroller displays search results, but they are pushed up, hiding the search bar, and two empty rows are added under the results, followed by one row that belongs in the regular tableview (it displays using the custom tableviewcell)

returning from detail view to the searchdisplaycontroller displays search results, but they are pushed up, hiding the search bar, and two empty rows are added under the results, followed by one row that belongs in the regular tableview (it displays using the custom tableviewcell)


selecting another search result displays the correct detail view

selecting another search result displays the correct detail view


returning from detail view to the searchdisplaycontroller displays search results, but they are pushed up more, hiding the search bar and top search results, and two empty rows are added under the results, followed by two rows that belong in the regular tableview (they are displayed using custom tableviewcells)

returning from detail view to the searchdisplaycontroller displays search results, but they are pushed up more, hiding the search bar and top search results, and two empty rows are added under the results, followed by two rows that belong in the regular tableview (they are displayed using custom tableviewcells)



Solution

  • Sounds like you have a method somewhere that scrolls the search bar off screen initially, and every time you return to the view controller that's in charge of the search display controller said method is called repeatedly, scrolling the table view higher and higher (possibly via viewWillAppear or viewDidAppear).

    If that's the case, you can check the state of the search display controller and only perform that method if it's not active, for example:

    if (!self.searchDisplayController.isActive) {
        // safe to scroll off-screen
    }
    

    Without seeing your code it's difficult to diagnose though.