【问题标题】:SearchController in iOS 11. Can't touch in any tableView celliOS 11 中的 SearchController。无法触摸任何 tableView 单元格
【发布时间】:2017-11-02 10:01:24
【问题描述】:

搜索控制器的系统行为:

  • 进入 iPhone 设置
  • 滑动显示搜索栏
  • 点击搜索栏
  • TableView 现在是灰色的,如果我触摸任何地方(不在 searchBar 中),searchBar 就会隐藏
  • 我输入任何文本,tableView 现在颜色正常,我可以点击进入任何搜索到的单元格。

在我的应用程序中,点击任何文本后 tableView 仍然是灰色的。我无法点击任何搜索到的单元格。 不同之处还在于,当我搜索时,我使用updateSearchResultsForSearchController 和 reloadData 更新旧的 tableView(搜索后我没有不同的 tableView)。

问题是如何隐藏这个灰色视图并在搜索控制器中输入任何文本后有机会点击单元格?

我创建搜索控制器的方式:

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;

以及我如何更新 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;
    }];
}

示例项目代码:https://pastebin.com/m9V2dunB

截图:

系统行为:

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

【问题讨论】:

  • @Tubelight 没有任何改变。
  • 请分享你的截图,我会澄清你想要什么?
  • @Tubelight 我的情况与第四点相同(在系统搜索设置中),但在放置文本之后也是如此。我会准备并分享截图。
  • @Tubelight 我添加了截图。我需要创建新项目并以相同的方式添加 tableView 和 searchBar。
  • @Tubelight 我也添加了示例代码。

标签: ios objective-c uitableview uisearchcontroller


【解决方案1】:

我通过将此行添加到 viewDidLoad 来解决您的演示项目中的问题

search.dimsBackgroundDuringPresentation = NO;

只要去掉UISearchController的暗色背景,一切都会好起来的。

【讨论】:

  • 你拯救了我的明天。正在运行,谢谢!
  • 欢迎我的朋友@PiotrWasilewicz ;)
  • 一点也不明显,在我设置此值之前无法访问我的范围按钮 - 你为我节省了一些头发 - 谢谢
  • @Nostradamus 我很高兴听到这个消息。上帝保佑你的头发(づ。◕‿‿◕。)づ
  • 对于 iOS 12 及更高版本,使用:search.obscuresBackgroundDuringPresentation = NO;
【解决方案2】:

您可以从界面生成器中设置 tableview 的选择属性。在 XIB 或 Storyboard 中选择您的 tableview,然后选择属性检查器并设置 single selectiontoselection` 属性,如下图所示。

删除代码中的这一行:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

【讨论】:

  • 我试过这段代码。它没有改变任何东西。搜索时仍无法单击单元格。
  • 你确定要像这样使用tabview方法selectRowAtIndexPath吗?
  • 改变它也没有帮助。搜索时未调用“selectRowAtIndexPath”。
  • 我添加了指向我的测试项目的链接。如果可以,请运行它。
  • 在您的代码中删除以下行:cell.selectionStyle = UITableViewCellSelectionStyleNone;
【解决方案3】:

我想在做出选择后关闭控制器,所以我这样做了:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多