博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS11 - UINavigationItem大标题,搜索栏实现
阅读量:5222 次
发布时间:2019-06-14

本文共 6097 字,大约阅读时间需要 20 分钟。

1.新建模型

class Contact: NSObject {    var name : String?    var mobile : String?{        didSet{            if mobile?.lengthOfBytes(using: .utf8) == 11 {                mobileString = mobile                var spaceIndex = mobile?.index(mobile!.startIndex, offsetBy: 3)                mobileString?.insert(" ", at: spaceIndex!)                spaceIndex = mobile?.index(mobile!.endIndex, offsetBy: -3)                mobileString?.insert(" ", at: spaceIndex!)            }        }    }    var address : String?    var email : String?    var mobileString : String?}

2.搜索控制器

class TestViewController: UIViewController,UISearchResultsUpdating{    func updateSearchResults(for searchController: UISearchController) {            }    var searchResultsCon = ResultsTableViewController()    override func viewDidLoad() {        super.viewDidLoad()        view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)    }
import UIKitclass ResultsTableViewController: UITableViewController {    var contacts = Array
() override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return contacts.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "reuseID") if contacts.count > indexPath.row { let contact = contacts[indexPath.row] cell.textLabel!.text = contact.mobileString } cell.textLabel!.textColor = UIColor.brown return cell }

3.主控制器

class ViewController: UITableViewController,UISearchResultsUpdating{    var searchResultsCon = ResultsTableViewController()    var contacts = Array
() override func viewDidLoad() { super.viewDidLoad() configureTableView() configureNavigationBar() } func configureTableView(){ tableView.estimatedRowHeight = 44.0 tableView.estimatedSectionHeaderHeight = 0.0 tableView.estimatedSectionFooterHeight = 0.0 for _ in 0...20 { let temp = Int(arc4random_uniform(1000000000)) + 15000000000 let contact = Contact() contact.mobile = String(temp) contacts.append(contact) } } func configureNavigationBar(){ if #available(iOS 11.0, *) { self.navigationController?.navigationBar.prefersLargeTitles = true //导航栏文本颜色 self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor:#colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)] self.navigationController?.navigationBar.largeTitleTextAttributes = [.foregroundColor:#colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)] //search let mySearchController: UISearchController = UISearchController(searchResultsController: searchResultsCon) mySearchController.searchResultsUpdater = self // 设置模态出结果页搜索前不隐藏导航栏// mySearchController.hidesNavigationBarDuringPresentation = false self.navigationItem.searchController = mySearchController // 搜索框默认显示并且滚动不消失 self.navigationItem.hidesSearchBarWhenScrolling = false //当模态出结果页搜索后不隐藏导航栏 self.definesPresentationContext = true //刷新控件 tableView.refreshControl = UIRefreshControl() tableView.refreshControl?.addTarget(self, action: #selector(self.refreshData), for: .valueChanged) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } //MARK -- tableView override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return contacts.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "reuseID") if contacts.count > indexPath.row { let contact = contacts[indexPath.row] cell.textLabel?.text = contact.mobileString } return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) self.navigationController?.pushViewController(TestViewController(), animated: true) } @objc func refreshData(){ DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2), execute: { self.tableView.refreshControl?.endRefreshing() }) } //MARK -- searchController @available(iOS 8.0, *) func updateSearchResults(for searchController: UISearchController) { let searchText = searchController.searchBar.text! let contactList = contacts.filter({ (con) -> Bool in return (con.mobile?.hasPrefix(searchText))! }) searchResultsCon.contacts = contactList searchResultsCon.tableView.reloadData() } override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { // 搜索框滚动时消失 self.navigationItem.hidesSearchBarWhenScrolling = true }}

 

转载于:https://www.cnblogs.com/edensyd/p/9523435.html

你可能感兴趣的文章
layui父页面执行子页面方法
查看>>
如何破解域管理员密码
查看>>
Windows Server 2008 R2忘记管理员密码后的解决方法
查看>>
IE11兼容IE8的设置
查看>>
windows server 2008 R2 怎么集成USB3.0驱动
查看>>
Foxmail:导入联系人
查看>>
vue:axios二次封装,接口统一存放
查看>>
vue中router与route的区别
查看>>
js 时间对象方法
查看>>
网络请求返回HTTP状态码(404,400,500)
查看>>
Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
查看>>
Mac下使用crontab来实现定时任务
查看>>
303. Range Sum Query - Immutable
查看>>
图片加载失败显示默认图片占位符
查看>>
【★】浅谈计算机与随机数
查看>>
《代码阅读方法与实现》阅读笔记一
查看>>
解决 sublime text3 运行python文件无法input的问题
查看>>
javascript面相对象编程,封装与继承
查看>>
Atlas命名空间Sys.Data下控件介绍——DataColumn,DataRow和DataTable
查看>>
Java中正则表达式的使用
查看>>