SearchController问题,搜索displayController时会显示搜索栏的间距
问题是:
在storyboard
,我必须取消选中Adjust Scroll View Insets
,因为如果不这样做,我将得到另一个问题( UISearchController问题,NSLayoutAttribute在真实设备中不起作用 ),我不知道这是否会影响问题这里。(我在模拟器中测试,如果选中Adjust Scroll View Insets
,此处的问题将不会出现)
我的代码
import UIKit import SVProgressHUD class ChooseStoreViewController: UIViewController,UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating { @IBOutlet weak var tableView: UITableView! var ori_dataSource: [StoreListModel] = [StoreListModel]() var dataSource = [String]() var filterdDataSource = [String]() var resultSearchController = UISearchController() var choosedStore:StoreListModel? = nil var userInfoFromChooseTerant:[String:Any]? @IBOutlet weak var top_constraint: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() initData() initUI() } // MARK: - view life override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.isNavigationBarHidden = false } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.navigationController?.isNavigationBarHidden = true } func initData() { // 1.配置self.resultSearchController = UISearchController(searchResultsController: nil) self.resultSearchController.searchResultsUpdater = self self.resultSearchController.dimsBackgroundDuringPresentation = false self.resultSearchController.searchBar.sizeToFit() self.resultSearchController.searchBar.placeholder = "搜索" self.resultSearchController.searchBar.tintColor = UIColor.black self.resultSearchController.searchBar.delegate = self self.tableView.tableHeaderView = self.resultSearchController.searchBar let nib = UINib(nibName: "TerantListCell", bundle: nil) // Required if our subclasses are to use: dequeueReusableCellWithIdentifier:forIndexPath: //tableView.register(nib, forCellReuseIdentifier: "TerantListCell") self.tableView.register(nib, forCellReuseIdentifier: "TerantListCell") self.tableView.tableFooterView = UIView.init() self.tableView.reloadData() networkForStoreList() } func initUI() { let backNavItem:UIBarButtonItem = UtilSwift.addBackButtonItem(nil, controlelr: self) backNavItem.action = #selector(navBack) // print(userInfoFromChooseTerant!) // 2.隐藏cell下的Line tableView.separatorStyle = UITableViewCellSeparatorStyle.none // 3.tableView } // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. let chooseRole: ChooseRoleViewController = segue.destination as! ChooseRoleViewController chooseRole.userInfoFromChooseStore = self.userInfoFromChooseTerant } // MARK: - search delegate func searchBarCancelButtonClicked() { for item:NSLayoutConstraint in self.tableView.constraints { self.view.setNeedsLayout() if item.firstAttribute == NSLayoutAttribute.top { item.constant = 0 } } } // MARK: - searchbar delegate func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { searchBar.setValue("取消", forKey:"_cancelButtonText") } func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { } // MARK: - private methods func navBack() { _ = self.navigationController?.popViewController(animated: true) } // MARK: - actions @IBAction func unwindToChooseStoreVCFromChooseRole(segue: UIStoryboardSegue){ } @IBAction func nextStepAction(_ sender: UIButton) { // 可以不选择门店,非必选/*if choosedStore == nil { let lml_alert: LMLDropdownAlertView = LMLDropdownAlertView.init(frame: self.view.bounds) lml_alert.showAlert(title: Global.鹿鸣提示标题, detail_Title: "请选择门店!", cancleButtonTitle: "取消", confirmButtonTitle: "确定", action: { (button) in }) return }*/ self.resultSearchController.isActive = false // 失活if self.choosedStore != nil { _ = self.userInfoFromChooseTerant?.updateValue(self.choosedStore!.userId, forKey: "store_id") } self.performSegue(withIdentifier: "ChooseStoreVCToChooseRoleVC", sender: self) } // MARK: - network func networkForStoreList() { let params:[String:String] = [ "createTime":"-1", "userId" : self.userInfoFromChooseTerant!["affiliated_id"] as! String ] // url_terantList Mysevers.afpost(withHud: true, andAddressname: Global.url_listStore, parmas: params, requestSuccess: { (result) in let stateCode = UtilSwift.getNetStateCode(result: result as Any, key: Global.net_key_stateCode) if stateCode == 0 { let storeArr:[[String : Any]] = UtilSwift.getNetAnyObject(result: result as Any, key: "list") as! [[String : Any]] // Global.net_key_bussiness 后台写错了//self.ori_dataSource = terantArr for item:[String: Any] in storeArr { let store_list_model: StoreListModel = StoreListModel.initStoreListModelWithDic(dic: item) self.ori_dataSource.append(store_list_model) } for item:StoreListModel in self.ori_dataSource { self.dataSource.append(item.name) } self.tableView.reloadData() // 刷新tableView }else if stateCode == -1 { // 弹窗系统错误SVProgressHUD.showError(withStatus: "系统错误") } }, failBlcok: { // 弹窗系统错误SVProgressHUD.showError(withStatus: "系统错误") }) } // MARK: - tableView func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if self.resultSearchController.isActive { return filterdDataSource.count }else { return dataSource.count } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: TerantListCell = tableView.dequeueReusableCell(withIdentifier: "TerantListCell", for: indexPath) as! TerantListCell // 配置cell if self.resultSearchController.isActive { cell.title_label.text = self.filterdDataSource[indexPath.row] }else { cell.title_label?.text = self.dataSource[indexPath.row] } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.choosedStore = self.ori_dataSource[indexPath.row] } // MARK: - regexp func updateSearchResults(for searchController: UISearchController) { self.filterdDataSource.removeAll(keepingCapacity: false) let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!) let array = (self.dataSource as NSArray).filtered(using: searchPredicate) self.filterdDataSource = array as! [String] self.tableView.reloadData() } }
转到存在“ChooseStoreViewController”的“.storyboard”文件。 然后单击UITableView并更改tableView约束,如下所示:
检查顶部空间约束。
- SceneKit视图呈现向后
- 分组了UITableView和水平页边距
- 如何检测touchbegan和触摸移动ios SWIFT中的angular度?
- 如何在Swift中创建抽象类
- 使用各种调度队列和NSOperationQueue处理执行时间和性能
- 未find'FBSDKLoginKit / FBSDKLoginKit.h'文件(Facebook SDK for iOS v4.2&Xcode 7)
- 如何使用Core Graphics(用于手指画)创build浮雕效果或阴影效果
- iOS:如何检测音乐是否在任何背景音乐应用程序中播放?
- 将iPhone上的UIImage传递给Apple Watch会导致手表无响应