swift searchBar

//

//  SearchViewController.swift

//  UIControlDemo

//

//  Created by  on 14/12/3.

//  Copyright (c) 2014 马大哈. All rights reserved.

//

 

import UIKit

 

class SearchViewController: BaseViewController , UITableViewDataSource, UITableViewDelegate ,UISearchBarDelegate{ 

    var searchBarControl: UISearchBar?

    var controlListTable:UITableView?

    var controlListArray = ["View","Lable","Button","ImageView","TextField+UIDatePicker+UIPickerView","TextView","UIActivityIndicatorView","UISlider+UIProgressView","UIScrollView+UIPageControl","UISegmentedControl+UISwitch","UIWebView","MKMapView","UIToolbar+alertview","UISearchController","UITableView","ApplicationDocument","Time/Date/UILocalNotification"]

    override func viewDidLoad() {

        super.viewDidLoad()

 

        controlListTable = UITableView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height), style: UITableViewStyle.Plain)

        controlListTable?.backgroundColor = .whiteColor();

        controlListTable?.delegate   = self

        controlListTable?.dataSource = self

        self.view.addSubview(controlListTable!)

    

        searchBarControl = UISearchBar(frame: CGRectMake(0.0, 0.0, 200 , 45))

        searchBarControl?.delegate = self;

        searchBarControl?.barStyle = .Default

        searchBarControl?.placeholder = "输入关键字"

        searchBarControl?.showsBookmarkButton = true

        

        /*

        searchBar放在tableheaderview  效果

        controlListTable?.tableHeaderView = searchBarControl

        */

        let rightBar = UIBarButtonItem(customView: searchBarControl!)

        self.navigationItem.rightBarButtonItem = rightBar; 

    }

    

    // 点击  取消  按钮

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {

        println("点击取消按钮")

        searchBar.showsCancelButton = false

        searchBar.resignFirstResponder()

        controlListArray = ["View","Lable","Button","ImageView","TextField+UIDatePicker+UIPickerView","TextView","UIActivityIndicatorView","UISlider+UIProgressView","UIScrollView+UIPageControl","UISegmentedControl+UISwitch","UIWebView","MKMapView","UIToolbar+alertview","UISearchController","UITableView","ApplicationDocument","Time/Date/UILocalNotification"]

        controlListTable?.reloadData()

    }

 

    // 点击  搜索  按钮

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {

        println("开始搜索")

        searchBar.resignFirstResponder()

        var tempArray = controlListArray

        controlListArray.removeAll()

        for temp in tempArray{

            if  temp == searchBar.text{

                controlListArray.append(temp)

                controlListTable?.reloadData()

                println("开始搜索参数 : (temp)")

            }

        }

    }

    

    // 即将输入textfield

    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {

        println("开始录入")

        searchBar.showsCancelButton = true

        searchBar.showsScopeBar = true        

        return true

    }

    

    func searchBarBookmarkButtonClicked(searchBar: UISearchBar) {

        println("点击搜索记录按钮,显示搜索历史.如果是iphone,可弹出新页面显示搜索记录。如iPad可使用popover")

        /*

        let time = TimeViewController()

        self.presentViewController(time, animated: true, completion: nil)

        */

    }

    

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return controlListArray.count;

    }

    

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 50

    }

    

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: nil)

        cell.textLabel?.text = String(format: "%d  %@",indexPath.row, controlListArray[indexPath.row])

        return cell

    }

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

}

 

 

原文地址:https://www.cnblogs.com/madaha/p/4159914.html