简体   繁体   中英

SearchController in iOS 11. Can't touch in any tableView cell

System behavior of the search controller:

  • Go to iPhone settings
  • Slide to show searchBar
  • Tap on searchBar
  • TableView has gray color now and if I touch anywhere (not in searchBar) searchBar will hide
  • I enter any text and tableView has normal color now, I can click into any of searched cells.

In my application, after tapped any text tableView is still gray. I can't tap in any of searched cells. The difference is also that when I searching I update old tableView (I don't have different tableView after searching) by using updateSearchResultsForSearchController and reloadData after that.

The question is how to hide this gray view and give chance to tap on cells after enter any text in searchcontroller?

The way I create search controller:

UISearchController * search = [[UISearchController alloc] initWithSearchResultsController:nil];
search.searchResultsUpdater = self;
self.navigationItem.searchController = search;
self.navigationItem.hidesSearchBarWhenScrolling = YES;
self.navigationItem.searchController.searchBar.barTintColor = [UIColor blueColor];
self.navigationItem.searchController.searchBar.delegate = self;

and how I update tableView cells:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    self.actualSearchController = searchController;
    self.filteredData = [self filterContentForSearchText:searchController.searchBar.text];
    [self.tableView reloadData];
}

- (NSMutableArray<MySectionContainter *> *)filterContentForSearchText:(NSString *)text {
    NSMutableArray<MySectionContainter *> *sections = [NSMutableArray array];

    for (MySectionContainter *section in self.tableData) {
        NSArray *objects = [sectionInfo.rowObjects filteredArrayUsingPredicate:[self.filterSource predicateForSearching:text]];

        if ([objects count] > 0) {
            [sections addObject:[[MySectionContainter alloc] initWithTitle:section.title andObjects:objects]];
        }
    }

    return sections;
}

- (NSPredicate *)predicateForSearching:(NSString *)text {
    return [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        if ([evaluatedObject isKindOfClass:[MyClass class]]) {
            return [((MyClass *)evaluatedObject).title containsString:text];
        }


    return NO;
    }];
}

Example project code: https://pastebin.com/m9V2dunB

Screenshot:

在此处输入图片说明

System behavior:

在此处输入图片说明

Test project: https://www.sendspace.com/file/2lhedj

I solve problem in your demo project by adding this line to viewDidLoad

search.dimsBackgroundDuringPresentation = NO;

Just remove dim background of UISearchController , everything will work fine.

You can set selection property of tableview from interface builder. Select your tableview in XIB or Storyboard then select attribute inspector and set single selectiontoselection` property like below screenshot.

在此处输入图片说明

Remove this line in your code:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

I wanted to dismiss the controller after making a selection, so this is what I did:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.view.endEditing(true)
    // 1. Dismiss search controller
    searchController.dismiss(animated: false, completion: nil)

    let record = fetchedResultsController.object(at: indexPath)
    delegate?.didSelectUser(record)
    // 2. Dismiss the UITableViewController
    dismiss(animated: true, completion: nil)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM