第一步:通过searchBarShouldBeginEditing解决,编辑的时候,cancel字体问题:
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
searchCurrentPage = 1
searchListArray = listArray
tableView.reloadData()
searchBar.setShowsCancelButton(true, animated: true);
let tempView = searchBar.subviews.first;
for subView: UIView in (tempView?.subviews)!{
if subView.isMember(of: NSClassFromString("UINavigationButton")!){
let cancelButton: UIButton = subView as! UIButton;
cancelButton.setTitle("取消", for: .normal);
break;
}
}
return true
}
第二步:通过searchBarSearchButtonClicked 点击搜索,执行网络请求
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if searchListArray.count > 0 {
searchListArray.removeAll()
}
// 执行网络请求,然后刷新视图
self.tableView.reloadData()
}
第三步:通过searchBarCancelButtonClicked 取消搜索状态
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchController.isActive = false
self.tableView.reloadData()
}
第四步:解决搜索框,弹出和消失的时候,动画不同步的问题
func willPresentSearchController(_ searchController: UISearchController) {
self.edgesForExtendedLayout = .bottom
}
func willDismissSearchController(_ searchController: UISearchController) {
self.edgesForExtendedLayout = .bottom
}
完整伪代码:
import UIKit
class OcssClientListVC: OcssBaseVC, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchControllerDelegate {
private let clientListIdentifier = "OcssClientListCell"
private var tableView: UITableView!
private var listArray = [[String: Any]]()
private var currentPage = 1
private var pageSize = 30
private var isHidden = false
private var searchCurrentPage = 1
private var searchListArray = [[String: Any]]()
private var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "商户列表"
let search = UISearchController(searchResultsController: nil)
search.dimsBackgroundDuringPresentation = false
self.searchController = search
search.searchBar.placeholder = "按商户ID搜索"
// search.searchResultsUpdater = self
search.searchBar.delegate = self
search.delegate = self
search.searchBar.sizeToFit()
tableView = UITableView.init(frame: view.bounds, style: .plain)
tableView.delegate = self
tableView.backgroundColor = UIConstants.ColorFromRGB(245, 245, 245)
tableView.dataSource = self
tableView.register(OcssClientListCell.self, forCellReuseIdentifier: clientListIdentifier)
tableView.separatorStyle = .none
view.addSubview(tableView)
tableView.tableFooterView = UIView()
tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: { [weak self] in
if let strongSelf = self {
strongSelf.getAgentList()
}
})
tableView.tableHeaderView = search.searchBar
self.definesPresentationContext = true
// self.searchVC.nav = self.navigationController;
// self.searchVC.searchBar = self.searchController.searchBar;
getAgentList()
moreButton.isHidden = false
moreButton.setTitle("创建", for: .normal)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isTranslucent = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.isTranslucent = true
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive {
return searchListArray.count
}
return listArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100.0 * UIConstants.NewScale_iPhone6
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:clientListIdentifier, for: indexPath) as! OcssClientListCell
cell.selectionStyle = .none
if searchController.isActive {
// cell 赋值
}else {
// cell 赋值
}
return cell
}
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
searchCurrentPage = 1
searchListArray = listArray
tableView.reloadData()
searchBar.setShowsCancelButton(true, animated: true);
let tempView = searchBar.subviews.first;
for subView: UIView in (tempView?.subviews)!{
if subView.isMember(of: NSClassFromString("UINavigationButton")!){
let cancelButton: UIButton = subView as! UIButton;
cancelButton.setTitle("取消", for: .normal);
break;
}
}
return true
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if searchListArray.count > 0 {
searchListArray.removeAll()
}
// 网络请求
self.tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.tableView.mj_footer.isHidden = isHidden
searchController.isActive = false
self.tableView.reloadData()
}
func willPresentSearchController(_ searchController: UISearchController) {
self.edgesForExtendedLayout = .bottom
}
func willDismissSearchController(_ searchController: UISearchController) {
self.edgesForExtendedLayout = .bottom
}
}