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

Mel*_*rne 6 search uitableview uisearchbar ios swift

我有一个带有搜索栏作为标题的 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

    self.delegate?.setCity(city: str);
    self.dismiss(animated: true, completion: nil);
}
Run Code Online (Sandbox Code Playgroud)

这是我的问题:在第一次点击单元格时,视图控制器不会关闭。我点击一次,搜索栏辞职响应者。我需要点击两次才能执行解雇。

这是我如何在 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);
Run Code Online (Sandbox Code Playgroud)

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

ran*_*dom 2

您可以将点击手势识别器添加到您的视图中,当触发时,它会

  1. 放弃搜索栏响应程序
  2. 将点击位置转换为表格视图中的点
  3. 获取点击位置的单元格
  4. 手动调用didSelectRowAt这不是“最佳”解决方案,但用于演示目的。

下面是它的实现方式:

override func viewDidLoad() {
        super.viewDidLoad()

        // Add a tap gesture to our view
        let gesture = UITapGestureRecognizer(target: self, action: #selector(TestTableViewController.didTap(_:)))
        self.view.addGestureRecognizer(gesture)
    }

    // Called when the tap gesture fires
    func didTap(gesture: UITapGestureRecognizer) {

        // We get rid of our keyboard on screen
        searchBar.resignFirstResponder()

        // Find the location of the touch relative to the tableView
        let touch = gesture.locationInView(self.tableView)

        // Convert that touch point to an index path
        if let indexPath = tableView.indexPathForRowAtPoint(touch) {

            // Here I am just calling the delegate method directly which you shouldn't do.
            // You should just do whatever you want to do with the indexPath here.
            tableView(tableView, didSelectRowAtIndexPath: indexPath)
        }
    }
Run Code Online (Sandbox Code Playgroud)