Swift - 搜索條(UISearchBar)的用法


1,搜索條Options屬性還可設置如下功能樣式:

Shows Search Results Button:勾選后,搜索框右邊顯示一個圓形向下的按鈕,單擊會發送特殊事件。
Shows Bookmarks Button:勾選后,搜索框右邊會顯示一個書本的按鈕,單擊會發送特殊事件。
Shows Cancel Button:勾選后,搜索框右邊會出現一個“Cancel”按鈕,單擊會發送特殊事件。
Shows Scope Bar:勾選后,會在搜索條下面出現一個分段控制器。
原文:Swift - 搜索條(UISearchBar)的用法

2,下面是一個搜索條的使用樣例,功能如下:
(1)在Main.storyboard界面里拖入一個 Search Bar 和一個 Table View,Search Bar放到Table View的頁眉位置
(2)初始化或者搜索條為空時,表格顯示所有數據
(3)搜索條不為空時,表格實時過濾顯示匹配的項目
 
3,效果圖
      原文:Swift - 搜索條(UISearchBar)的用法       原文:Swift - 搜索條(UISearchBar)的用法
 
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
71
72
73
74
75
import  UIKit
 
class  ViewController UIViewController , UISearchBarDelegate ,
UITableViewDataSource , UITableViewDelegate  {
     
     // 引用通過storyboard創建的控件
     @IBOutlet  var  searchBar :  UISearchBar !
     @IBOutlet  var  tableView :  UITableView !
     
     // 所有組件
     var  ctrls:[ String ] = [ "Label" , "Button1" , "Button2" , "Switch" ]
     // 搜索匹配的結果,Table View使用這個數組作為datasource
     var  ctrlsel:[ String ] = []
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         
         // 起始加載全部內容
         self .ctrlsel =  self .ctrls
         //設置代理
         self .searchBar.delegate =  self
         self .tableView.delegate =  self
         self .tableView.dataSource =  self
         // 注冊TableViewCell
         self .tableView.registerClass( UITableViewCell . self ,
             forCellReuseIdentifier:  "SwiftCell" )
     }
     
     // 返回表格行數(也就是返回控件數)
     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 = tableView.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 ) {
         print (searchText)
         // 沒有搜索內容時顯示全部組件
         if  searchText ==  ""  {
             self .ctrlsel =  self .ctrls
         }
         else  // 匹配用戶輸入內容的前綴(不區分大小寫)
             self .ctrlsel = []
             for  ctrl  in  self .ctrls {
                 if  ctrl.lowercaseString.hasPrefix(searchText.lowercaseString) {
                     self .ctrlsel.append(ctrl)
                 }
             }
         }
         // 刷新Table View顯示
         self .tableView.reloadData()
     }
     
     // 搜索代理UISearchBarDelegate方法,點擊虛擬鍵盤上的Search按鈕時觸發
     /**func searchBarSearchButtonClicked(searchBar: UISearchBar) {
         searchBar.resignFirstResponder()
     }**/
     
     override  func  didReceiveMemoryWarning() {
         super .didReceiveMemoryWarning()
     }
}


原文出自:www.hangge.com  轉載請保留原文鏈接:http://www.hangge.com/blog/cache/detail_562.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM