5

I am working on adding a search bar in to an existing app.

I have a table that is populated with data downloaded from a server, and I am using the new UISearchController.

I have the search bar all fully working now, and displaying a new filtered table of results as the user types into the search bar.

My question is how to handle the user selecting an item from this new filtered table of search results?

I added a new segue from my filtered table and added a didSelectRowAtIndexPath, which does work fine - but when a user selects an item from the filtered table, the search bar remains, and clicking cancel after that point crashes the app.

So I am not sure what I am supposed to do and how I am supposed to handle a user selecting items from the filtered table?

Do I keep things the way I have them, but add some code to cancel the search bar when the user selects an item?

Or am I doing this wrong, and there is a way to hand the selection back from the filtered table back to the main viewcontroller table on user selection of a filtered item?

Any assistance, as always, very gratefully received!

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
R2D2
  • 2,620
  • 4
  • 24
  • 46

4 Answers4

9

After initializing the searchController, try setting the following property of searchController that will enable didSelectRowAtIndexPath method of UITableViewDelegate

searchController.obscuresBackgroundDuringPresentation = false
Gary M
  • 428
  • 5
  • 13
Mohammed Shakeer
  • 1,446
  • 16
  • 23
1

Try dismissing the Search Bar when the user selects the filtered table row:

[yourSearchController.searchBar resignFirstResponder];
Gjchoza
  • 187
  • 1
  • 7
  • I don't think that will dismiss the search controller. I would use `[yourSearchController dismissViewControllerAnimated:YES completion:nil];` – Kenneth P. Hough Jan 30 '17 at 05:44
1

Any selection of a table, as you are aware, is performed under the method

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

All you need to do is add a condition in the method didSelectRowAtIndexPath: to distinguish between the "full" data table versus the "filtered" data table.

In the example code below I use a Singleton named UserData to store my selection, but there are many different implementations, and suggestions are welcome. (I won't go into what singletons are but if you are interested, check out this blog by Matt Galloway).

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (mySearchController.active) {
        // if the search bar is active use filteredData array
        [[UserData sharedUserData] setSelection:filteredData[indexPath.row]];
        // here you can add your segue code
    } else {
        // if search bar is not active then use the full data set
        [[UserData sharedUserData] setSelection:unfilteredData[indexPath.row]];
        // here you can add your segue code
    }

    // Optional:
    //  if you would like to add a checkmark for selection
    // first remove any existing checkmarks
    for (UITableViewCell *cell in [tableView visibleCells]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    // then add new check mark
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    // and lastly tell the selected cell to deselect
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

I hope this helps. Let me know if you have any questions or any suggestions to improve this. :)

Kenneth P. Hough
  • 577
  • 2
  • 8
  • 25
0

You're probably trying to present the new controller from the table controller. Don't do it that way, you should present it from the search bar controller.

Pablo A.
  • 2,042
  • 1
  • 17
  • 27
  • Just pass the table selection to the search bar controller, you can use a custom delegate method. Once the search controller gets the delegate call you can present the new controller from there. Here you can check how to create a delegate http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c – Pablo A. Aug 24 '15 at 12:02