• 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()

        }

    }

     

     

  • 相关阅读:
    .NET Core开发实战(第10课:环境变量配置提供程序)--学习笔记
    .NET Core开发实战(第9课:命令行配置提供程序)--学习笔记
    .NET Core开发实战(第8课:配置框架:让服务无缝适应各种环境)--学习笔记
    .NET Core开发实战(第7课:用Autofac增强容器能力)--学习笔记(下)
    .NET Core开发实战(第7课:用Autofac增强容器能力)--学习笔记(上)
    .NET Core开发实战(第6课:作用域与对象释放行为)--学习笔记(下)
    .NET Core开发实战(第6课:作用域与对象释放行为)--学习笔记(上)
    .NET Core开发实战(第5课:依赖注入:良好架构的起点)--学习笔记(下)
    .NET Core开发实战(第5课:依赖注入:良好架构的起点)--学习笔记(中)
    .NET Core开发实战(第5课:依赖注入:良好架构的起点)--学习笔记(上)
  • 原文地址:https://www.cnblogs.com/madaha/p/4159914.html
Copyright © 2020-2023  润新知