1,搜索条Options属性还可设置如下功能样式:

Shows Search Results Button:勾选后,搜索框右边显示一个圆形向下的按钮,单击会发送特殊事件。
Shows Bookmarks Button:勾选后,搜索框右边会显示一个书本的按钮,单击会发送特殊事件。
Shows Cancel Button:勾选后,搜索框右边会出现一个“Cancel”按钮,单击会发送特殊事件。
Shows Scope Bar:勾选后,会在搜索条下面出现一个分段控制器。


2,下面是一个搜索条的使用样例,功能如下:
(1)在Main.storyboard界面里拖入一个Search Bar和一个Table View,Search Bar放到Table View的页眉位置
(2)初始化或者搜索条为空时,表格显示所有数据
(3)搜索条不为空时,表格实时过滤显示匹配的项目

3,效果图

4,代码如下
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
import UIKit
class ViewController : UIViewController , UISearchBarDelegate ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
UITableViewDataSource UITableViewDelegate {
// 引用通过storyboard创建的控件
@IBOutlet var searchBar : UISearchBar !
tableView : UITableView !
// 所有组件
ctrls:[ String ] = [ "Label" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, "Button1" "Button2" "Switch" ]
// 搜索匹配的结果,Table View使用这个数组作为datasource
ctrlsel:[ ] = []
override func viewDidLoad() {
super .viewDidLoad()
// 起始加载全部内容
self .ctrlsel = .ctrls
// 注册TableViewCell
.tableView.registerClass( UITableViewCell . ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forCellReuseIdentifier: "SwiftCell" )
}
// 返回表格行数(也就是返回控件数)
tableView(tableView: ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,numberOfRowsInSection section: Int ) -> {
return .ctrlsel.count
}
// 创建各单元显示内容(创建参数indexPath指定的单元)
NSIndexPath )
-> UITableViewCell
{
// 为了提供表格显示性能,已创建完成的单元需重复使用
let identify: = "SwiftCell"
// 同一形式的单元格重复使用,在声明时已注册
cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath)
as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType . disclosureIndicator
cell.textLabel?.text = .ctrlsel[indexPath.row]
return cell
}
// 搜索代理UISearchBarDelegate方法,每次改变搜索内容时都会调用
searchBar(searchBar: !,textDidChange searchText: !) {
// 没有搜索内容时显示全部组件
if searchText == "" {
.ctrls
}
else { // 匹配用户输入内容的前缀
.ctrlsel = []
for ctrl in .ctrls {
ctrl.lowercaseString.hasPrefix(searchText) {
.ctrlsel.append(ctrl)
}
}
}
// 刷新Table View显示
.tableView.reloadData()
}
// 搜索代理UISearchBarDelegate方法,点击虚拟键盘上的Search按钮时触发
//func searchBarSearchButtonClicked(searchBar: UISearchBar!) {
//searchBar.resignFirstResponder()
//}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// dispose of any resources that can be recreated.
}
}

原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_562.html

Swift - 搜索条UISearchBar的用法的更多相关文章

  1. web字体加载方案优化小结

    这篇文章主要介绍了web字体加载方案优化小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  2. ios – 如何在使用PhoneGap构建的iPhone应用程序中使用Google Web Font?

    我们希望在为iOS设备构建的PhoneGap应用程序中使用GoogleWebFonts,但我们希望在应用程序包中托管字体文件,而不是使用代码通过Web动态获取字体.如果设备处于脱机状态,除非将字体文件捆绑到应用程序中,否则字体将不可用.谷歌提供下载网络字体的能力,但我们如何引用HTML应用程序内的本地字体文件?解决方法我们将字体文件放在.ttf格式的某个地方的assets/www文件夹中.宣言:然后像这样使用它:

  3. UISearchBar的取消和清除按钮在iOS 7中不起作用

    谢谢解决方法我有同样的问题,我尝试使用以下代码.请试试这个.

  4. UISearchBar AutoLayout ios7错误?

    我正在使用AutoLayout并且有一个UISearchBar,UITableView广告和UIView.最初布局是正确的,并根据我的AutoLayout规则定位.当UISearchBar有焦点时,它的高度突然扩大,覆盖了我的顶级UIView.无论我如何布置我的自动布局限制,都会发生这种情况.如果我没有设置并设置一个框架,然后它的行为正常.但我依赖于iOS7应用程序中的AutoLayout.这是一个真正的错误吗?或者很可能我错过了什么!解决方法有同样的问题.在viewDidLoad中尝试这个

  5. ios – UISearchBar在开始编辑时会拉伸文本

    我有一个UISearchBar的实例添加到UINavigationBar的标题视图中.当已经设置了文本并且搜索栏开始编辑时,它会调整其内容的大小以允许“取消”按钮的空间,但是,生成的动画会拉伸文本,如下面的gif所示有什么办法可以避免这种缺陷效应吗?

  6. ios7 – UISearchbar barTintColor取消按钮不可见

    我尝试将UISearchbar的条形颜色设置为一种颜色,将tintColor设置为另一种颜色,代码如下:barTintColor正确应用.问题是:取消按钮的文本不可见,但如果在故事板中尝试相同,一切都按预期工作.解决方法我注意到取消按钮只有在状态为“正常”时才能看到,所以我找到了解决问题的代码,但我不认为这是预期的方法.

  7. ios – 隐藏UISearchController中搜索栏上的“取消”按钮

    我试图隐藏UISearchController中搜索栏的“取消”按钮,但不幸的是在viewDidLoad()中设置以下内容不起作用:我也试过在这个委托方法中使用上面的代码:这种方法有效但在隐藏之前会短暂显示取消按钮,这并不理想.有什么建议?

  8. ios – UISearchBar和/或UISearchDisplayController颜色

    解决方法看看你的sampleproject

  9. ios – UICollectionView不能使用UISearchController?

    在WWDC2014年的“AInsideInsidePresentationControllers”中,演示者展示了如何在UITableView中设置UISearchController.他们通过设置searchController的searchBar框架,然后将其设置为tableView的tableHeaderView来实现.不幸的是,UICollectionView没有相当于tableHeade

  10. ios – 如何更改UISearchBar占位符和图像色调?

    我一直在尝试搜索结果几个小时,但我不能得到这个想法.也许是不可能的.我试图改变UISearchBar的占位符文字和放大镜的色调颜色.如果重要的话,我只针对iOS8.0.这是我的代码,现在看起来如何我希望搜索和放大镜是白色的,或者是深绿色.解决方法如果您有可以使用的自定义图像,则可以使用类似于以下内容的方式设置图像并更改占位符文本颜色:在这个例子中,我使用了purpleColor,而您可以使用(UI

随机推荐

  1. Swift UITextField,UITextView,UISegmentedControl,UISwitch

    下面我们通过一个demo来简单的实现下这些控件的功能.首先,我们拖将这几个控件拖到storyboard,并关联上相应的属性和动作.如图:关联上属性和动作后,看看实现的代码:

  2. swift UISlider,UIStepper

    我们用两个label来显示slider和stepper的值.再用张图片来显示改变stepper值的效果.首先,这三个控件需要全局变量声明如下然后,我们对所有的控件做个简单的布局:最后,当slider的值改变时,我们用一个label来显示值的变化,同样,用另一个label来显示stepper值的变化,并改变图片的大小:实现效果如下:

  3. preferredFontForTextStyle字体设置之更改

    即:

  4. Swift没有异常处理,遇到功能性错误怎么办?

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. 字典实战和UIKit初探

    ios中数组和字典的应用Applicationschedule类别子项类别名称优先级数据包contactsentertainment接触UIKit学习用Swift调用CocoaTouchimportUIKitletcolors=[]varbackView=UIView(frame:CGRectMake(0.0,0.0,320.0,CGFloat(colors.count*50)))backView

  6. swift语言IOS8开发战记21 Core Data2

    上一话中我们简单地介绍了一些coredata的基本知识,这一话我们通过编程来实现coredata的使用。还记得我们在coredata中定义的那个Model么,上面这段代码会加载这个Model。定义完方法之后,我们对coredata的准备都已经完成了。最后强调一点,coredata并不是数据库,它只是一个框架,协助我们进行数据库操作,它并不关心我们把数据存到哪里。

  7. swift语言IOS8开发战记22 Core Data3

    上一话我们定义了与coredata有关的变量和方法,做足了准备工作,这一话我们来试试能不能成功。首先打开上一话中生成的Info类,在其中引用头文件的地方添加一个@objc,不然后面会报错,我也不知道为什么。

  8. swift实战小程序1天气预报

    在有一定swift基础的情况下,让我们来做一些小程序练练手,今天来试试做一个简单地天气预报。然后在btnpressed方法中依旧增加loadWeather方法.在loadWeather方法中加上信息的显示语句:运行一下看看效果,如图:虽然显示出来了,但是我们的text是可编辑状态的,在storyboard中勾选Editable,再次运行:大功告成,而且现在每次单击按钮,就会重新请求天气情况,大家也来试试吧。

  9. 【iOS学习01】swift ? and !  的学习

    如果不初始化就会报错。

  10. swift语言IOS8开发战记23 Core Data4

    接着我们需要把我们的Rest类变成一个被coredata管理的类,点开Rest类,作如下修改:关键字@NSManaged的作用是与实体中对应的属性通信,BinaryData对应的类型是NSData,CoreData没有布尔属性,只能用0和1来区分。进行如下操作,输入类名:建立好之后因为我们之前写的代码有些地方并不适用于coredata,所以编译器会报错,现在来一一解决。

返回
顶部