I think I found a better implementation for this problem. All of the previous answers correctly show a dim view that is identical to what the UITableView looks like before the search, but for each solution there is not enough functionality to touch the area to cancel the search.
For this reason, I believe this code works better.
First of all, create a BOOL, such as searchButtonTapped, to indicate whether the search button has been clicked. By default it is NO.
Then:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { if (!searchButtonTapped) {
This should now be clear based on the other answers. Remember to also restore the original settings when the user clicks the Search button.
In addition, in the cellForIndexPath method, add:
cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.contentView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];
To create the same dim picture that appears before entering text. Make sure that you apply these properties to the right cell, that is, check which UITableView is active and that the user has not clicked the search button.
Then, to a decisive extent, in didSelectRowAtIndexPath:
if (tableView == self.searchDisplayController.searchResultsTableView) { if (searchButtonTapped) { // Code for when the user select a row after actually having performed a search { else [self.searchDisplayController setActive:NO animated:YES];
The user can now click on a dim area that does not result in a visible selection of UITableViewCell, but instead cancels the search.
Willem Sleegers Nov 01 2018-11-11T00: 00Z
source share