88
99import UIKit
1010
11- class MasterViewController : UITableViewController , UISearchBarDelegate , UISearchDisplayDelegate {
11+ class MasterViewController : UITableViewController , UISearchResultsUpdating {
1212
13+ var filteredNotifications = [ NotificationData] ( )
14+ var resultSearchController = UISearchController ( )
15+
1316 override func viewDidLoad( ) {
1417 super. viewDidLoad ( )
1518
@@ -22,35 +25,26 @@ class MasterViewController: UITableViewController, UISearchBarDelegate, UISearch
2225
2326 self . tableView. estimatedRowHeight = 44.0
2427 self . tableView. rowHeight = UITableViewAutomaticDimension
28+
29+ self . resultSearchController = ( {
30+ let controller = UISearchController ( searchResultsController: nil )
31+ controller. searchResultsUpdater = self
32+ controller. dimsBackgroundDuringPresentation = true
33+ controller. searchBar. sizeToFit ( )
34+
35+ self . tableView. tableHeaderView = controller. searchBar
36+
37+ return controller
38+ } ) ( )
39+
40+ // Reload the table
41+ self . tableView. reloadData ( )
2542 }
26-
27-
28- var filteredNotifications = [ NotificationData] ( )
29-
30- func filterContentForSearchText( searchText: String ) {
31- // Filter the array using the filter method
32- self . filteredNotifications = notifications. filter ( { ( notification: NotificationData ) -> Bool in
33- // let categoryMatch = (scope == "All") || (notification.alert == scope)
34- let stringMatch = notification. alert. rangeOfString ( searchText)
35- return ( stringMatch != nil )
36- } )
37- }
38- func searchDisplayController( controller: UISearchDisplayController , shouldReloadTableForSearchString searchString: String ? ) -> Bool {
39- self . filterContentForSearchText ( searchString!)
40- return true
41- }
42-
43- func searchDisplayController( controller: UISearchDisplayController , shouldReloadTableForSearchScope searchOption: Int ) -> Bool {
44- self . filterContentForSearchText ( self . searchDisplayController!. searchBar. text!)
45- return true
46- }
47-
48-
43+
4944 override func didReceiveMemoryWarning( ) {
5045 super. didReceiveMemoryWarning ( )
5146 // Dispose of any resources that can be recreated.
5247 print ( " didReceiveMemoryWarning " )
53-
5448 }
5549
5650
@@ -63,11 +57,12 @@ class MasterViewController: UITableViewController, UISearchBarDelegate, UISearch
6357 }
6458
6559 override func tableView( tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
66- if tableView == self . searchDisplayController! . searchResultsTableView {
67- print ( " filtered " )
60+ if ( self . resultSearchController . active ) {
61+ print ( " filtered " )
6862 return self . filteredNotifications. count
69- } else {
70- print ( " unfiltered " )
63+ }
64+ else {
65+ print ( " unfiltered " )
7166 return notifications. count
7267 }
7368 }
@@ -77,8 +72,8 @@ class MasterViewController: UITableViewController, UISearchBarDelegate, UISearch
7772 var notification : NotificationData
7873
7974 notification = notifications [ indexPath. row]
80-
81- if tableView == self . searchDisplayController! . searchResultsTableView {
75+
76+ if ( self . resultSearchController . active ) {
8277 notification = filteredNotifications [ indexPath. row]
8378 } else {
8479 notification = notifications [ indexPath. row]
@@ -96,6 +91,26 @@ class MasterViewController: UITableViewController, UISearchBarDelegate, UISearch
9691
9792 return cell
9893 }
94+
95+
96+ func updateSearchResultsForSearchController( searchController: UISearchController )
97+ {
98+ print ( updateSearchResultsForSearchController)
99+ print ( searchController. searchBar. text!)
100+
101+ // filteredNotifications.removeAll(keepCapacity: false)
102+ if searchController. searchBar. text! == " " {
103+ filteredNotifications = notifications
104+ } else {
105+ self . filteredNotifications = notifications. filter ( { ( notification: NotificationData ) -> Bool in
106+ // let categoryMatch = (scope == "All") || (notification.alert == scope)
107+ let stringMatch = notification. alert. rangeOfString ( searchController. searchBar. text!)
108+ return ( stringMatch != nil )
109+ } )
110+ }
111+ self . tableView. reloadData ( )
112+ }
113+
99114}
100115
101116
0 commit comments