ios - 双击需要用搜索栏选择 TableView 项目

标签 ios swift uitableview search uisearchbar

我有一个带有搜索栏作为标题的 UITableView。 当用户在搜索栏中进行搜索时,我使用此功能更新我的数据。

func updateSearchResults(for searchController: UISearchController) {

    if let searchText = searchController.searchBar.text {
        if (searchText.characters.count > 0) {
            self.filteredResults  = [];
            self.locationManager?.geocodeAddressString(addressString: searchText, completionHandler: { (results, error) in
                if error == nil && results != nil {
                    self.filteredResults = results!;
                    self.tableView.reloadData();
                }
            });
        }
        else {
            self.filteredResults  = [];
            self.tableView.reloadData();
        }
    }
}

当我在我的 UITableView 中选择一个单元格时,这个功能。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.delegate?.setCity(city: str);
    self.dismiss(animated: true, completion: nil);
}

这是我的问题: View Controller 不会在第一次点击单元格时关闭。我点击一次,搜索栏退出响应程序。我需要点击两次才能执行解雇。

下面是我如何在 viewDidLoad 中链接我的 tableview 和我的搜索栏:

// Search Bar componants
self.searchController = UISearchController(searchResultsController: nil);
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false;

self.searchBar = self.searchController.searchBar;
self.searchBar.searchBarStyle = .default;
self.searchBar.delegate = self;
self.searchBar.placeholder = NSLocalizedString("search.city", comment: "search.city").capitalized;

self.tableView = UITableView(frame: CGRect.zero, style: .plain);
self.tableView.translatesAutoresizingMaskIntoConstraints = false;
self.tableView.delegate = self;
self.tableView.backgroundColor = UIColor.white;
self.tableView.dataSource = self;
self.tableView.tableHeaderView = self.searchBar;
self.view.addSubview(self.tableView);

如果有人能帮助我,那就太好了:)

最佳答案

Swift 4+ 或 iOS 10+

1- 移除 viewDidLoad 或其他地方的搜索覆盖。

searchController.obscuresBackgroundDuringPresentation = false

2- 在 TableView didSelectRowAt 委托(delegate)中添加这一行。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     searchController.isActive = false
     <your code>
}

..完成..

注意:在 iOS 11+ 中,如果您在导航项中使用 SearchController,则不会出现此问题。如果您将 ViewController 与 searchController 一起显示,只需使用您的 ViewController 添加一个新的导航。并改为显示新导航。

UINavigationController(rootViewController: ViewControllerWithSearchBar) 

关于ios - 双击需要用搜索栏选择 TableView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40178206/

相关文章:

ios - UITableview在每行出现之前无法平滑滚动?

ios - 如果不在 Swift 的内部范围内使用,是否不保留闭包变量?

ios - 如何使表格 View 的单元格选择基于步进器值,而不是 swift 3 中单元格的物理选择?

ios - 如何在 ios objective c 中管理状态

xcode - 在现代 Swift OSX 应用程序中组织 XIB 和 Storyboard

swift - 将距离测量格式设置为公里或英制英里,而不是公制英里

ios - Swift - 运算符后的预期表达式(错误)

ios - 在 if-else 之外返回单元格

ios - Linking.openUrl 在 IOS 中发送短信

ios - 切换选项卡时WKWebview PDF问题