You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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.
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".
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.
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
@@ -60,13 +42,11 @@ var filteredTableData = [String]()
60
42
var resultSearchController = UISearchController()
61
43
```
62
44
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
let controller = UISearchController(searchResultsController: nil)
@@ -84,12 +64,10 @@ override func viewDidLoad() {
84
64
}
85
65
```
86
66
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
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
@@ -150,13 +120,9 @@ Finally, implement the updateSearchResultsForSearchController delegate method of
150
120
}
151
121
```
152
122
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.
0 commit comments