简体   繁体   English

iOS 11中的SearchController。无法触摸任何tableView单元格

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

System behavior of the search controller: 搜索控制器的系统行为:

  • Go to iPhone settings 转到iPhone设置
  • Slide to show searchBar 滑动以显示searchBar
  • Tap on searchBar 点击搜索栏
  • TableView has gray color now and if I touch anywhere (not in searchBar) searchBar will hide TableView现在为灰色,如果我触摸任何地方(不在searchBar中),searchBar将隐藏
  • I enter any text and tableView has normal color now, I can click into any of searched cells. 我输入任何文本,并且tableView现在具有正常颜色,我可以单击任何搜索到的单元格。

In my application, after tapped any text tableView is still gray. 在我的应用程序中,点击任何文本后tableView仍为灰色。 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. 区别还在于,当我搜索时,我会通过使用updateSearchResultsForSearchController和reloadData之后更新旧的tableView(搜索后没有不同的tableView)。

The question is how to hide this gray view and give chance to tap on cells after enter any text in searchcontroller? 问题是如何在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: 以及如何更新tableView单元格:

- (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 示例项目代码: https : //pastebin.com/m9V2dunB

Screenshot: 截图:

在此处输入图片说明

System behavior: 系统行为:

在此处输入图片说明

Test project: https://www.sendspace.com/file/2lhedj 测试项目: https//www.sendspace.com/file/2lhedj

I solve problem in your demo project by adding this line to viewDidLoad 我通过将这一行添加到viewDidLoad来解决您的演示项目中的问题

search.dimsBackgroundDuringPresentation = NO;

Just remove dim background of UISearchController , everything will work fine. 只需删除UISearchController背景,一切都会正常。

You can set selection property of tableview from interface builder. 您可以从界面构建器设置tableview的selection属性。 Select your tableview in XIB or Storyboard then select attribute inspector and set single selectiontoselection` property like below screenshot. 在XIB或Storyboard中选择表格视图,然后选择属性检查器并设置单个selectiontoselection`属性,如下面的屏幕截图所示。

在此处输入图片说明

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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