Skip to content

Commit bd112a2

Browse files
committed
search bar for tableview translated
1 parent 7346d8f commit bd112a2

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
Add Search to Table View Tutorial in iOS8 with Swift
2+
---
3+
在iOS8中给TableView加一个搜索栏(Swift)
4+
---
5+
> * 原文链接 : [Add Search to Table View Tutorial in iOS8 with Swift](http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift)
6+
* 原文作者 : [Arthur Knopper](https://twitter.com/ioscreator)
7+
* 译文出自 : [开发技术前线 www.devtf.cn](http://www.devtf.cn)
8+
* 译者 : [StormXX](https://github.com/StormXX)
9+
* 校对者: 暂无
10+
* 状态 : 等待校对
11+
12+
Most modern iOS apps have a feature of a search bar to search for items in a Table View. This tutorial will show how to add the search functionality to a Table View which let the user search through the items by specifying a search term. In previous iOS versions a UISearchDisplayController object was used to implement searching, as of iOS 8 this class is deprecated, UISearchController must be used instead, which is what we use in this tutorial. This tutorial is built in iOS 8.4 and Xcode 6.3
13+
14+
现在大多数的iOS应用都会在TableView上放一个搜索框来提供一个搜索内容的入口。本篇教程将会讲述如何在TableView中添加一个搜索功能,能让用户来搜索整个TableView中的内容。在以前的iOS版本中,都是使用一个UISearchDisplayController对象来实现搜索。然而在iOS8中,这个类被弃用了,UISearchController被用来代替它。本篇教程也将会教大家使用UISearchController.
15+
注:此教程的开发环境是iOS 8.3,Xcode 6.3,Swift 1.2.
16+
17+
Open Xcode and create a new Single View Application. For product name, use IOS8SwiftAddSearchTableViewTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.
18+
19+
打开Xcode然后创建一个新的**Single View Application**。在**product name**这一栏填写IOS8SwiftAddSearchTableViewTutorial 然后在Organization Name和Organization Identifier填写你自己的值。编程语言选择Swift然后在Devices选择iPhone。
20+
21+
![](http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/54d7b6bde4b00ffa20f1df7a/1423423166651/?format=1500w)
22+
23+
Remove the View Controller from the Storyboard and drag a Navigation Controller to the empty canvas. When the initial View Controller is deleted there isn't a starting point defined. Select the Navigation Controller and go to the Attribute Inspector. In the View Controller Section select the "Is Initial View Controller" checkbox.
24+
25+
上面的做好之后,打开Storyboard,移除自动生成的ViewController,然后拖拽一个NavigationController到Storyboard里。当刚刚的"初始"ViewController被删除了之后,这个APP就没有“起点”了。选择刚刚拖进来的NavigationController然后进到右边的Attribute Inspector。在 View Controller这一个节点下勾选”Is Initial View Controller“
26+
27+
![](http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/54d7b766e4b08579c6ac7813/1423423335054/?format=750w)
28+
29+
Double-click on the Navigation Bar in The Table View Controller and set the title to "Numbers". Select the Table View Cell and go to the Attributes Inspector. In the Table View Cell section set the Identifier to "Cell".
30+
31+
双击左边 TableViewController 的导航栏,把它的title设为“Numbers”。选择TableViewCell然后到右边的 Attributes Inspector 面板,把 Cell 的 Indentifier 设为"Cell"。
32+
33+
![](http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/54d7b7dae4b002f56c3e6ca4/1423423451428/?format=750w)
34+
35+
The Storyboard should look like this.
36+
37+
Storyboard现在看起来应该像下面这样
38+
39+
![](http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/54d7b879e4b0ff7e52fca235/1423423610868/?format=2500w)
40+
41+
Since we have deleted the View Controller from the Storyboard we can also delete the ViewController.swift file. Add a new file to the project, select iOS->Source->Cocoa Touch Class. Name it TableViewController and make it a subclass of UITableViewController.
42+
43+
因为我们删掉了自动生成的ViewController,所以我们也可以删掉ViewController.swift。添加一个新的swift文件到项目中,选择**iOS->Source->Cocoa Touch Class**。把它命名为TableViewController 然后设置它为UITableViewController的子类。
44+
45+
![](http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/54d7b8a7e4b0b30d9474144a/1423423656532/?format=1500w)
46+
47+
Go to the TableViewController.swift file and change the class declaration line
48+
49+
点开TableViewController.swift文件,把类的声明这一行改成下面这样:
50+
51+
`class TableViewController: UITableViewController, UISearchResultsUpdating {`
52+
53+
The UISearchResultsUpdating protocol updates the search results, later we will add a delegate method to let the TableViewController conform to this protocol. Add the following properties
54+
55+
UISearchResultsUpdating协议用来更新搜索结果,稍后我们将会添加一个代理方法来让TableViewController继承这个协议。现在在这个类中添加下列属性:
56+
57+
```
58+
let tableData = ["One","Two","Three","Twenty-One"]
59+
var filteredTableData = [String]()
60+
var resultSearchController = UISearchController()
61+
```
62+
63+
The tableData property will hold the items of the Table View, where the filteredTableData property will contain the result from the search query. The Search Controller manages the results of the search. Change the viewDidLoad method to
64+
65+
这个`tableData`属性用来存储TableView的数据,`filteredTableData`属性用来存储搜索后的结果数据。Search Controller控制搜索的结果,在viewDidLoad中添加下列代码:
66+
67+
```
68+
override func viewDidLoad() {
69+
super.viewDidLoad()
70+
71+
self.resultSearchController = ({
72+
let controller = UISearchController(searchResultsController: nil)
73+
controller.searchResultsUpdater = self
74+
controller.dimsBackgroundDuringPresentation = false
75+
controller.searchBar.sizeToFit()
76+
77+
self.tableView.tableHeaderView = controller.searchBar
78+
79+
return controller
80+
})()
81+
82+
// Reload the table
83+
self.tableView.reloadData()
84+
}
85+
```
86+
87+
A closure is used which is assigned to the resultSearchController. The results of the search will be presented in the current Table View, so the searchResultsController parameter of the UISearchController init method is set to nil. Also, the searchResultsUpdater property is set to the current Table View Controller and the background dimming is set to false. The searchable is added to the Table View and the data is reloaded. Next, implement the Table View Data Source delegate methods
88+
89+
resultSearchController的初始化使用了一个闭包。搜索的结果将会在当前的TableView上展示,所以searchResultsController这个参数在UISearchController的初始化方法中被设置成nil。还有,searchResultsUpdater属性被设置成了当前的Table View Controller,background dimming也被设置成false。这个搜索被加到Table View上,数据也被重新加载。接下来,实现TableView的Data Source代理方法:
90+
91+
```
92+
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
93+
// 1
94+
// Return the number of sections.
95+
return 1
96+
}
97+
98+
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
99+
// 2
100+
if (self.resultSearchController.active) {
101+
return self.filteredTableData.count
102+
}
103+
else {
104+
return self.tableData.count
105+
}
106+
}
107+
108+
109+
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
110+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
111+
112+
// 3
113+
if (self.resultSearchController.active) {
114+
cell.textLabel?.text = filteredTableData[indexPath.row]
115+
116+
return cell
117+
}
118+
else {
119+
cell.textLabel?.text = tableData[indexPath.row]
120+
121+
return cell
122+
}
123+
}
124+
```
125+
126+
1. Only One section is being used
127+
2. If the Search Controller is active the number of filtered items are assigned to the number of rows in the Table View, otherwise the tableData array is being used.
128+
3. If the Search Controller is active, the filtered items are displayed, otherwise the original items of the tableData array are displayed.
129+
130+
Finally, implement the updateSearchResultsForSearchController delegate method of the UISearchResultsUpdating protocol
131+
132+
1. 这个TableView中只有一个Section
133+
2. 当Search Controller被激活,搜索结果的数目被赋值到numberOfRows方法上,否则就使用tableData这个数组。
134+
3. 当Search Controller被激活,所搜结果被展示出来,否则就使用tableData的数据来展示。
135+
136+
最后,实现`UISearchResultsUpdating`协议中的`updateSearchResultsForSearchController`代理方法。
137+
138+
139+
140+
```
141+
func updateSearchResultsForSearchController(searchController: UISearchController)
142+
{
143+
filteredTableData.removeAll(keepCapacity: false)
144+
145+
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
146+
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
147+
filteredTableData = array as! [String]
148+
149+
self.tableView.reloadData()
150+
}
151+
```
152+
153+
The updateSearchResultsForSearchController method is called when the user updates the Search bar with input. In this method we will handle the search filtering of our search term. NSPredicate is an sort of expression that handles the search criteria. "c" means case-sensitive. The results are then assigned to the filteredTableData array and the Table View is reloaded. Build and Run the project, enter a search query and the results are being displayed.
154+
155+
当用户在搜索框输入的时候,`updateSearchResultsForSearchController`被调用。在这个方法里。我们处理我们的搜索条件。`NSPredicate`是一个用来放置筛选表达式的类,我们用它来存储搜索条件。“c”意思是区分大小写。筛选的结果被赋值给filteredTableData然后TableView调用了reloadData方法。编译运行这个项目,然后在搜索框输入搜索关键字,结果就会被展示出来。
156+
157+
![](http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/54d7c286e4b00ffa20f21dbc/1423426183530/?format=1000w)
158+
159+
You can download the source code of the IOS8SwiftAddSearchTableViewTutorial at the ioscreator repository on Github.
160+
161+
你可以下载`IOS8SwiftAddSearchTableViewTutorial`的源代码在ioscreator的[Github](https://github.com/ioscreator/ioscreator)上。
162+

0 commit comments

Comments
 (0)