双击选择带有搜索栏的TableView项目

时间:2016-10-21 13:40:20

标签: 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);
}

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

以下是我在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);

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

2 个答案:

答案 0 :(得分:1)

Swift 4+或iOS 10 +

1-删除viewDidLoad或其他位置的搜索叠加。

searchController.obscuresBackgroundDuringPresentation = false

2-在TableView didSelectRowAt委托中添加此行。

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

..完成..

注意:在iOS 11+中,如果在导航项中使用SearchController,则不会出现此问题。如果为ViewController提供searchController,则只需在ViewController中添加新的导航即可。并显示新的导航。

UINavigationController(rootViewController: ViewControllerWithSearchBar) 

答案 1 :(得分:0)

您可以在视图中添加点击手势识别器,以便在触发时

  1. 辞职搜索栏响应者
  2. 将点击位置转换为表格视图中的某个位置
  3. 获取点击位置的单元格
  4. 手动调用didSelectRowAt这不是“最佳”解决方案,但出于演示目的。
  5. 以下是它的实现方式:

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