首页 > 解决方案 > 如何在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
}

标签: iosswiftuitableview

解决方案


推荐阅读