Swift系列之——UISearchBar的简单使用

Swift系列之——UISearchBar的简单使用,第1张

概述本篇博客的语法适用于Swift3.0以上。 UISearchBar是一个苹果自带的搜索条,由一个文本框和几个按钮组成,当用户在文本框内输入部分内容之后,程序即可按照指定的规则执行搜索。 下面是UISearchBar的长相: //// ViewController.swift// UISearchBarTest//// Created by Mac on 2017/8/4.// Copyr

本篇博客的语法适用于Swift3.0以上。

UISearchbar是一个苹果自带的搜索条,由一个文本框和几个按钮组成,当用户在文本框内输入部分内容之后,程序即可按照指定的规则执行搜索。

下面是UISearchbar的长相:

//// VIEwController.swift// UISearchbarTest//// Created by Mac on 2017/8/4.// copyright © 2017年 Jing. All rights reserved.//import UIKitclass VIEwController: UIVIEwController,UItableVIEwDelegate,UItableVIEwDataSource,UISearchbarDelegate {    var tableArray: NSArray = ["UI","UIVIEw","VIEw","VIEwController","Controller","UIVIEwController","Search","UISearchbar","Swift"]    var searchArray: NSArray = []    var isSearch = false//默认在非搜索状态下    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        // Do any additional setup after loading the vIEw,typically from a nib.        self.tableVIEw.tableheaderVIEw = self.searchbar;        self.vIEw.addSubvIEw(tableVIEw);    }    overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()        // dispose of any resources that can be recreated.    }    //使用懒加载方式来创建UItableVIEw    lazy var tableVIEw: UItableVIEw = {        let temptableVIEw = UItableVIEw (frame: self.vIEw.bounds,style: UItableVIEwStyle.plain)        temptableVIEw.delegate = self        temptableVIEw.dataSource = self        temptableVIEw.tableFooterVIEw = UIVIEw.init()        return temptableVIEw    }()    //使用懒加载方式来创建UISearchbar    lazy var searchbar: UISearchbar = {        let tempSearchbar = UISearchbar(frame:CGRect(x: 0,y: 64,wIDth: self.vIEw.bounds.size.wIDth,height: 40))// tempSearchbar.prompt = "查找图书";        tempSearchbar.placeholder = "请输入搜索关键字";        tempSearchbar.showsCancelbutton = true;        tempSearchbar.delegate = self        return tempSearchbar    }()    //根据输入的关键字来过滤搜索结果    func filterBySubstring(filterStr: Nsstring!) {        isSearch = true        let predicate = nspredicate(format: "SELF CONTAINS[c] %@",filterStr)        searchArray = tableArray.filtered(using: predicate) as NSArray        tableVIEw.reloadData()    }    //MARK: UItableVIEwDelegate    func numberOfSections(in tableVIEw: UItableVIEw) -> Int {        return 1    }    func tableVIEw(_ tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int {        if isSearch {            return searchArray.count        }        else{            return tableArray.count        }    }    func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell {        let IDentifIEr = "cellID"        var cell = tableVIEw.dequeueReusableCell(withIDentifIEr: IDentifIEr)        if cell == nil {            cell = UItableVIEwCell.init(style: UItableVIEwCellStyle.default,reuseIDentifIEr: IDentifIEr)        }        let row = indexPath.row        if isSearch {            cell?.textLabel?.text = searchArray[row] as? String        }        else{            cell?.textLabel?.text = tableArray[row] as? String        }        return cell!    }    //MARK: UISearchbarDelegate    func searchbarCancelbuttonClicked(_ searchbar: UISearchbar) {        isSearch = false        searchbar.resignFirstResponder()        tableVIEw.reloadData()    }    func searchbarSearchbuttonClicked(_ searchbar: UISearchbar) {        filterBySubstring(filterStr: searchbar.text! as Nsstring)    }    func searchbar(_ searchbar: UISearchbar,textDIDChange searchText: String) {        filterBySubstring(filterStr: searchText as Nsstring)    }}
总结

以上是内存溢出为你收集整理的Swift系列之——UISearchBar的简单使用全部内容,希望文章能够帮你解决Swift系列之——UISearchBar的简单使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1060094.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存