双击需要选择带有搜索栏的TableView项 [英] Double tap necessary to select TableView item with Search Bar
问题描述
我有一个带有搜索栏作为标题的UITableView.当用户在搜索栏中进行搜索时,我使用此功能来更新我的数据.
I have a UITableView with a search bar as header. I use this function to update my data when the user does a search in the search bar.
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中选择一个单元格时的此功能.
and this function when I select a cell in my UITableView.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate?.setCity(city: str);
self.dismiss(animated: true, completion: nil);
}
这是我的问题:在单元的第一次轻按时,视图控制器不会关闭.我轻按一次,搜索栏将响应者辞职.而且我需要轻按两次以执行解雇.
Here is my problem : the view controller doesn't dismiss at the first taps on the cell. I taps once, the search bar resign responder. And I need to tap twice for the dismiss to execute.
这是在viewDidLoad中链接表视图和搜索栏的方式:
Here is how I link my tableview and my search bar in 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);
如果有人可以帮助我,那就太好了:)
If someone could help me, that would be great :)
推荐答案
您可以向视图中添加点击手势识别器,以使其在发射时会
You can add a tap gesture recognizer to your view that when fired would
- 辞去搜索栏响应者
- 将点击位置转换为表格视图中的一个点
- 获取点击位置的单元格
- 手动调用
didSelectRowAt
,它不是最佳"解决方案,但用于演示目的.
- resign the search bar responder
- convert the tap location to a point in you table view
- get the cell that tap location
- manually call
didSelectRowAt
which isn't the "best" solution but for demonstration purposes.
以下是其实现方式:
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)
}
}
这篇关于双击需要选择带有搜索栏的TableView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!