ios - 如何在Swift中点击可扩展表格单元格时自动隐藏键盘
问题描述
场景是用户在其中搜索数据,UISearchController
在过滤数据后,用户将点击一个expandable cell
(对于我使用的可扩展单元格FoldingCell
)中的数据。但是当用户点击可扩展单元格时,键盘仍然可见。当用户点击可扩展单元格时,如何自动隐藏键盘?我只是在这里搜索可能的技术,但它不适用于我的应用程序。您可以推荐我可以使用的任何参考或解决方案吗?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! FoldingCell
if cell.isAnimating() {
return
}
var duration = 0.0
if cellHeights[(indexPath as NSIndexPath).row] == kCloseCellHeight { // open cell
cellHeights[(indexPath as NSIndexPath).row] = kOpenCellHeight
cell.unfold(true, animated: true, completion: nil)
duration = 0.3
} else {// close cell
cellHeights[(indexPath as NSIndexPath).row] = kCloseCellHeight
cell.unfold(false, animated: true, completion: nil)
duration = 0.5
}
UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: { () -> Void in
tableView.beginUpdates()
tableView.endUpdates()
}, completion: nil)
self.view.endEditing(true)
}
配置搜索栏
func configureSearchBar() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.resignFirstResponder()
searchController.searchBar.textColor = UIColor.black
searchController.searchBar.placeholder = "Search by name, department or employee number"
searchController.searchBar.searchBarStyle = .prominent
searchController.searchBar.barTintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255, alpha: 1.0)
searchController.searchBar.tintColor = UIColor.black
searchController.searchBar.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255, alpha: 1.0)
searchController.searchBar.setImage(#imageLiteral(resourceName: "search-2"), for: .search, state: .highlighted)
searchController.searchBar.setShowsCancelButton(false, animated: true)
searchController.searchBar.isTranslucent = true
self.ParticipantTableView.tableHeaderView = searchController.searchBar
}
解决方案
推荐阅读
- r - 在 R 中运行 KNN,使用分类和数值预测器
- angular - Angular 可排序列表和 ngFor 最后一个元素参考
- socialengine - How to include external files in a SocialEngine module
- ruby-on-rails - 如何在 ActiveRecord 中的描述中编写查询
- json - 用 boost 读取 json
- java - Spring Boot 和 Jersey,不能让 ExceptionMapper 工作
- html - 带有 2 个项目列的 CSS 水平滚动
- c# - C# NEST 查询无法正确打印数据
- javascript - 如何制作发送参与服务器邀请链接的表情符号跳跃命令
- python - 递归函数没有返回正确的值