1.新建模型

class Contact: NSObject {
var name : String?
var mobile : String?{
didSet{
if mobile?.lengthOfBytes(using: .utf8) == {
mobileString = mobile
var spaceIndex = mobile?.index(mobile!.startIndex, offsetBy: )
mobileString?.insert(" ", at: spaceIndex!)
spaceIndex = mobile?.index(mobile!.endIndex, offsetBy: -)
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 UIKit

class ResultsTableViewController: UITableViewController {
var contacts = Array<Contact>()
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
} 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<Contact>()
override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
configureNavigationBar()
}
func configureTableView(){
tableView.estimatedRowHeight = 44.0
tableView.estimatedSectionHeaderHeight = 0.0
tableView.estimatedSectionFooterHeight = 0.0
for _ in ... {
let temp = Int(arc4random_uniform()) +
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: )]
self.navigationController?.navigationBar.largeTitleTextAttributes = [.foregroundColor:#colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: )]
//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(), 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
}
}

最新文章

  1. 绘制扇形效果线条小Bug解决
  2. C和指针 第十五章 错误报告perror和exit
  3. 在WPF中使用变通方法实现枚举类型的XAML绑定
  4. 收集的在线图片压缩(jpg/png)
  5. css 图片内容在不同分辨率下居中显示(演示的图片宽度是1920px,当图片宽度大于显示屏的宽度时)
  6. springmvc No mapping found for HTTP request with URI in Dispatc
  7. TCP 连接的 TIME_WAIT 过多 导致 Tomcat 假死
  8. 万网云解析设置二级域名解析到同IP不同端口
  9. Python中的__new__()方法的使用
  10. TS流格式(转)
  11. DB天气安卓客户端测试计划
  12. IE6下整站的bug详解
  13. 盼盼Degenerate——清除浮动的方法
  14. 自己动手修改Robotium代码(上)
  15. ASP.NET部分代码示例
  16. UNIX网络编程——心跳包
  17. Runnable与Callable区别
  18. mysql问题汇总——持续更新
  19. 程序编译运行和exe运行之文件位置的区别
  20. pygame(class类)调用视图的方法

热门文章

  1. redis缓冲与数据库
  2. android mvp高速开发框架介绍(dileber的简单介绍)
  3. PythonCookBook笔记——数据结构和算法
  4. bb=Discuz与 Discuz! X ,Discuz!NT区别
  5. bc - An arbitrary precision calculator language
  6. 今日头条Go建千亿级微服务的实践
  7. ElasticSearch(十一)批量CURD bulk
  8. 【操作系统】使用BCD工具安装Ubuntu操作系统
  9. LLVM的总结
  10. Dispatch Sources(转)