(注:自iOS8起,苹果便废弃UISearchDisplayController的使用,改为使用UISearchController来实现类似功能,可参考我的另一篇文章“Swift - 使用UISearchController实现带搜索栏的表格”)

UISearchDisplayController控件默认封装了Search Bar和Table View,可同时提供搜索和结果表格显示功能。

下面提供了一个使用样例,同时通过代码定制Search Bar的一些属性来实现自定义的外观和效果,并且展示用于选择搜索范围的分段条的用法。
效果图如下:
    
    
--- ViewController.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import UIKit
 
class ViewController: UIViewController,UISearchBarDelegate {
     
    // 引用通过storyboard创建的控件
    @IBOutlet var searchDisplay: UISearchDisplayController!
     
    // 所有组件
    var ctrls:[String] = ["Label","Button1-初级","Button1-高级","Button2-初级","Button2-高级","Switch"]
     
    // 搜索匹配的结果,Table View使用这个数组作为datasource
    var ctrlsel:[String] = []
     
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
         
        // 起始加载全部内容
        self.ctrlsel = self.ctrls
        // 注册TableViewCell
        self.searchDisplay.searchResultsTableView.registerClass(UITableViewCell.self,
            forCellReuseIdentifier: "SwiftCell")
        // 设置文本提示
        self.searchDisplay.searchBar.placeholder = "输入搜索信息"
        // 可以设置初始值
        //self.searchDisplay.searchBar.text = "b"
        // 设置搜索栏提示信息
        self.searchDisplay.searchBar.prompt = "搜索组件名称"
        // 不显示Search Bar边框
        self.searchDisplay.searchBar.searchBarStyle = UISearchBarStyle.Minimal
        // 显示分段条
        self.searchDisplay.searchBar.showsScopeBar = true
        self.searchDisplay.searchBar.scopeButtonTitles = ["全部","初级","高级"]
    }
     
    // 返回表格行数(也就是返回控件数)
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.ctrlsel.count
    }
     
    // 创建各单元显示内容(创建参数indexPath指定的单元)
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!)
        -> UITableViewCell!
    {
        // 为了提供表格显示性能,已创建完成的单元需重复使用
        let identify:String = "SwiftCell"
        // 同一形式的单元格重复使用,在声明时已注册
        let cell = self.searchDisplay.searchResultsTableView.dequeueReusableCellWithIdentifier(
            identify, forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.textLabel?.text = self.ctrlsel[indexPath.row]
        return cell
    }
     
    // 搜索代理UISearchBarDelegate方法,每次改变搜索内容时都会调用
    func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {
        self.searchText = searchText
        searchCtrls()
    }
     
    // 选择分段条时调用
    func searchBar(searchBar: UISearchBar!, selectedScopeButtonIndexDidChange selectedScope: Int) {
        println(selectedScope)
        searchCtrls();
    }
    // 保存搜索内容
    var searchText:String = ""
     
    // 搜索过滤
    func searchCtrls() {
        // 没有搜索内容时显示全部组件
        if self.searchText == "" {
            self.ctrlsel = self.ctrls
        }
        else {
            var scope = self.searchDisplay.searchBar.selectedScopeButtonIndex;
            // 匹配用户输入内容的前缀
            self.ctrlsel = []
            for ctrl in self.ctrls {
                let lc = ctrl.lowercaseString
                if lc.hasPrefix(self.searchText) {
                    if (scope == 0 || (scope == 1 && lc.hasSuffix("初级"))
                        || (scope == 2 && lc.hasSuffix("高级"))) {
                        self.ctrlsel.append(ctrl)
                    }
                }
            }
        }
         
        // 不需要刷新Table View显示
        // self.searchDisplay.searchResultsTableView.reloadData()
    }
         
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

--- Main.storyboard ---

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="tne-QT-ifu">
            <objects>
                <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="SwiftInAction_008_013" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                        <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <searchBar contentMode="redraw" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="514-A1-KsH">
                                <rect key="frame" x="0.0" y="28" width="320" height="44"/>
                                <textInputTraits key="textInputTraits"/>
                                <connections>
                                    <outlet property="delegate" destination="BYZ-38-t0r" id="HNb-H1-SIO"/>
                                </connections>
                            </searchBar>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                    </view>
                    <connections>
                        <outlet property="searchDisplay" destination="Lnz-5r-UWS" id="vbY-sQ-4L2"/>
                        <outlet property="searchDisplayController" destination="Lnz-5r-UWS" id="Kly-uU-7J1"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
                <searchDisplayController id="Lnz-5r-UWS">
                    <connections>
                        <outlet property="delegate" destination="BYZ-38-t0r" id="889-9H-eic"/>
                        <outlet property="searchBar" destination="514-A1-KsH" id="jg7-8Q-dht"/>
                        <outlet property="searchContentsController" destination="BYZ-38-t0r" id="S5V-Yk-ta3"/>
                        <outlet property="searchResultsDataSource" destination="BYZ-38-t0r" id="5rb-QW-jlM"/>
                        <outlet property="searchResultsDelegate" destination="BYZ-38-t0r" id="9rR-uv-SDW"/>
                    </connections>
                </searchDisplayController>
            </objects>
        </scene>
    </scenes>
</document>

Swift - 带结果列表的搜索条(UISearchDisplayController)的用法的更多相关文章

  1. Swift - 搜索条(UISearchBar)的用法

    1,搜索条Options属性还可设置如下功能样式: Shows Search Results Button:勾选后,搜索框右边显示一个圆形向下的按钮,单击会发送特殊事件. Shows Bookmark ...

  2. UI UISearchBar UISearchDisplayController实现搜索条、解析颜色

    本文转载至 http://blog.sina.com.cn/s/blog_bf2d33bd01017q6l.html @interface ThirdViewController : UIViewCo ...

  3. 点击搜索取消UISearchDisplayController的搜索状态

    一般,我们用到UISearchDisplayController的时候,都是须要对一个数据源进行刷选,在UISearchDisplayController自带的tableView中展示出来,然后点击退 ...

  4. iOS 搜索条使用详解

    在ios开发中搜索条的使用挺常见的,不过之前一直没用到也没细细研究,最近做外包项目的时候刚好用到,在这里记录一下使用的过程,只要理解了原理,其实还是比较简单的!上传的图片有点大,刚好可以看清楚它的使用 ...

  5. 模仿京东顶部搜索条效果制作的一个小demo

    最近模仿京东顶部搜索条效果制作的一个小demo,特贴到这里,今后如果有用到可以参考一下,代码如下 #define kScreenWidth [UIScreen mainScreen].bounds.s ...

  6. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...

  7. 使用CSS3和jQuery可伸缩的搜索条

    使用CSS3和jQuery可伸缩的搜索条 helloweba.com 作者:月光光 时间:2013-12-09 21:23 标签: CSS3 jquery 搜索条在我们网站是必不可少的,尤其是在有限的 ...

  8. 一个漂亮的DIV搜索条

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. AIO5物料清单(BOM)列表,搜索编码和名称无效

    问题: AIO5物料清单(BOM)列表,搜索编码和名称无效.如图: 原因分析 AIO5的BOM是在二次开发平台上做的,在制作自定义单据的时候[查询参数设置]没有设置.如下图: 解决方案 工具:AIO5 ...

随机推荐

  1. jquery插件讲解:轮播(SlidesJs)+验证(Validation)

    SlidesJs(轮播支持触屏)——官网(http://slidesjs.com) 1.简介 SlidesJs是基于Jquery(1.7.1+)的响应幻灯片插件.支持键盘,触摸,css3转换. 2.代 ...

  2. NET Core 以及与 .NET Framework

    简析.NET Core 以及与 .NET Framework的关系 简析.NET Core 以及与 .NET Framework的关系 一 .NET 的 Framework 们 二 .NET Core ...

  3. DELPHI语法基础学习笔记-Windows 句柄、回调函数、函数重载等(Delphi中很少需要直接使用句柄,因为句柄藏在窗体、 位图及其他Delphi 对象的内部)

    函数重载重载的思想很简单:编译器允许你用同一名字定义多个函数或过程,只要它们所带的参数不同.实际上,编译器是通过检测参数来确定需要调用的例程.下面是从VCL 的数学单元(Math Unit)中摘录的一 ...

  4. JAVA中extends 与implements有啥区别?

    JAVA中extends 与implements有啥区别?1. 在类的声明中,通过关键字extends来创建一个类的子类.一个类通过关键字implements声明自己使用一个或者多个接口.extend ...

  5. const与define的使用区别

    1.const用于类成员变量定义,一旦定义且不能改变其值.define定义全局常量,在任何地方都可以访问. 2.define不能在类中定义而const可以. 3.const不能在条件语句中定义常量 i ...

  6. 高性能MySql进化论(四):Summary,Cache,Counter表的使用

    在实际的应用中,往往会定期的对一个周期内的系统数据进行统计分析.例如某购物网站定期的统计商品在一个月/年期内的销售情况,如果采用扫描所有相关表的方式在某个时间点进行统计分析, 由于数据量很大,以及表结 ...

  7. 深入浅出Hadoop Mahout数据挖掘实战(算法分析、项目实战、中文分词技术)

    Mahout简介 Mahout 是 Apache Software Foundation(ASF) 旗下的一个开源项目, 提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发人员更加方便快捷地创建 ...

  8. hdu 2871 Memory Control(伸展树splay tree)

    hdu 2871 Memory Control 题意:就是对一个区间的四种操作,NEW x,占据最左边的连续的x个单元,Free x 把x单元所占的连续区间清空 , Get x 把第x次占据的区间输出 ...

  9. uva 568(数学)

    题解:从1開始乘到n,由于结果仅仅要最后一位.所以每乘完一次,仅仅要保留后5位(少了值会不准确,刚開始仅仅保留了一位.结果到15就错了,保留多了int会溢出,比方3125就会出错) 和下一个数相乘,接 ...

  10. UVA 6480 Zombie Invasion(模拟退火)

    A group of survivors has arrived by helicopter to an isolated island. The island is made up of a lon ...