【问题标题】:Double tap necessary to select TableView item with Search Bar双击需要使用搜索栏选择 TableView 项目
【发布时间】:2016-10-21 13:40:20
【问题描述】:

我有一个带有搜索栏作为标题的 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 中链接我的表格视图和搜索栏:

// 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);

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

【问题讨论】:

  • 只是在黑暗中拍摄,如果您先切换语句并关闭视图,它会按预期工作吗?
  • 没有。我真的看起来像第一次点击只是结束搜索栏编辑
  • 如果您在didSelectRowAt 中设置断点,断点会在第一次点击时命中吗?

标签: ios swift uitableview search uisearchbar


【解决方案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,就不会出现这个问题。如果您使用 searchController 显示 ViewController,只需使用您的 ViewController 添加一个新导航。而是呈现新的导航。

UINavigationController(rootViewController: ViewControllerWithSearchBar) 

【讨论】:

    【解决方案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)
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多