UIKit應用 - Swift 版本: 1.使用UISearchController製做一個簡單的本地搜索應用

在前面, 咱們講解了不少 UIKit 的控件知識, 如今讓咱們來簡單的應用一下, 看看咱們是怎麼去使用這些 UIKit 進行 App 的開發, 廢話少說, 讓咱們直接來進入主題~javascript


1.搭建頁面

1

我這裏是使用了一個 VIewController 做爲控制器, 而後把 ViewController 交給了 UINavigationController 管理, 關於怎麼快速交給 UINavigationController 管理, 在以前的文章我演示過, 在此次再作一次.php

2


2.實現代碼

遵照代理協議, 數據源協議java

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
}

設置數據, 實例化UISearchControllerruby

var tableData = ["guangdong", "guangxi", "guangnan", "guangbei", "shanghai", "xiahai", "hunan", "hubei", "hudong", "huxi"]
    var filteredData:[String] = []
    var resultSearchController = UISearchController()

實現UITableView的代理方法以及數據源方法markdown

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if resultSearchController.active {
            return filteredData.count
        } else  {
            return tableData.count
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as! UITableViewCell

        if resultSearchController.active {
            cell.textLabel?.text = filteredData[indexPath.row]
        } else {
            cell.textLabel?.text = tableData[indexPath.row]
        }

        return cell
    }

實現UISearchController的搜索更新方法ide

// 當 UISearchController 變成第一響應者的時候調用
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        // 刪除 filteredData 裏的全部元素, 而且默認的保存爲 false
        filteredData.removeAll(keepCapacity: false)

        // 輸入篩選文本, 獲取以後會自動去內存中搜索關鍵字, SELF CONTAINS[c] 這句話是匹配規則的寫法, 直接照抄就能夠了
        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)

        // 從 tableData 中篩選輸入的關鍵字
        let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)

        // 再把篩選出來的關鍵字加入到filtereData
        filteredData = array as! [String]

        // 刷新TableView
        self.myTableView.reloadData()
    }

設置UISearchControllerui

func serachController() -> UISearchController{
        // 實例化 UISearchController, 而且設置搜索控制器爲 nil
        let controller = UISearchController(searchResultsController: nil)

        // 設置更新搜索結果的對象爲 self
        controller.searchResultsUpdater = self

        // 設置 UISearchController 是否在編輯的時候隱藏NavigationBar, 默認爲 true
        controller.hidesNavigationBarDuringPresentation = false

        // 設置 UISearchController 是否在編輯的時候隱藏背景色, 默認爲 true
        controller.dimsBackgroundDuringPresentation = false

        // 設置 UISearchController 搜索欄的 UISearchBarStyle 爲Prominent
        controller.searchBar.searchBarStyle = UISearchBarStyle.Prominent

        // 設置 UISearchController 搜索欄的 Size 是自適應
        controller.searchBar.sizeToFit()

        // UISearchController能夠設置在三個地方
        // 1.設置 navigationItem 的 titleView 爲 UISearchController
        self.navigationItem.titleView = controller.searchBar

        // 2.設置 TableView 的 tableHeaderView 爲controller.searchBar
        // self.myTableView.tableHeaderView = controller.searchBar

        // 3.設置 TableView 的偏移量, 使 UISearchController 默認就隱藏在 NavigationBar 下
            // self.myTableView.contentOffset = CGPointMake(0, CGRectGetHeight(controller.searchBar.frame))
        // 以上三種方法任選一種便可, 這裏默認的是第一種

        // 返回設置好的 UISearchController
        return controller
        }

最後在viewDidLoad 中實現全部方法spa

override func viewDidLoad() {
        super.viewDidLoad()

        myTableView.delegate = self
        myTableView.dataSource = self

        resultSearchController = serachController()
    }

3.最終效果圖

第一種
1代理

第二種
2code

第三種
3


好了, 此次就講到這裏, 下次咱們繼續~~

相關文章
相關標籤/搜索