• IOS11


    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 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 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<Contact>()
        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
        }
    }
  • 相关阅读:
    缓存(二)
    缓存
    SQL Server 导入大数据脚本
    C#执行OracleHelper
    MERGE 用法
    c# 高效读写文件
    C#网络编程
    C#常用IO流与读写文件
    注册asp.net 4.0 到iis
    javascript原生图片懒加载
  • 原文地址:https://www.cnblogs.com/edensyd/p/9523435.html
Copyright © 2020-2023  润新知