加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:10:09 所属栏目:百科 来源:网络整理
导读:本篇博客的语法适用于Swift3.0以上。 UISearchBar是一个苹果自带的搜索条,由一个文本框和几个按钮组成,当用户在文本框内输入部分内容之后,程序即可按照指定的规则执行搜索。 下面是UISearchBar的长相: // // ViewController.swift // UISearchBarTest //

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

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

下面是UISearchBar的长相:

//
// ViewController.swift
// UISearchBarTest
//
// Created by Mac on 2017/8/4.
// Copyright ? 2017年 Jing. All rights reserved.
//

import UIKit

class 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)
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读