Skip to content

Commit dde263e

Browse files
stevetreaseSteve Trease
authored andcommitted
Revised search that actually works this time!
1 parent 898961c commit dde263e

File tree

6 files changed

+59
-46
lines changed

6 files changed

+59
-46
lines changed

Swift Push 4/AppDelegate.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2222

2323
UIDevice.currentDevice().batteryMonitoringEnabled = true
2424

25-
let item = NotificationData()
25+
var item = NotificationData()
2626
item.alert = "Swift Push (\(versionNumber!)) starting on " + UIDevice.currentDevice().name
2727
notifications.insert(item, atIndex: 0)
2828

29+
let formatter = NSNumberFormatter()
30+
formatter.numberStyle = NSNumberFormatterStyle.SpellOutStyle
31+
for i in 1...10 {
32+
item = NotificationData()
33+
item.alert = "test data " + formatter.stringFromNumber(i)!
34+
notifications.insert(item, atIndex: 0)
35+
}
36+
2937
switch (application.applicationState) {
3038
case UIApplicationState.Active:
3139
print ("didFinishLaunchingWithOptions - active")

Swift Push 4/Base.lproj/Main.storyboard

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,9 @@
3030
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
3131
<animations/>
3232
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
33-
<searchBar key="tableHeaderView" contentMode="redraw" showsCancelButton="YES" id="Q08-01-f5b">
34-
<rect key="frame" x="0.0" y="64" width="600" height="44"/>
35-
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
36-
<animations/>
37-
<textInputTraits key="textInputTraits"/>
38-
<connections>
39-
<outlet property="delegate" destination="7bK-jq-Zjz" id="UMO-9S-oP0"/>
40-
</connections>
41-
</searchBar>
4233
<prototypes>
4334
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="Cell" textLabel="Arm-wq-HPj" detailTextLabel="su6-nF-v8b" style="IBUITableViewCellStyleSubtitle" id="WCw-Qf-5nD">
44-
<rect key="frame" x="0.0" y="130" width="600" height="44"/>
35+
<rect key="frame" x="0.0" y="86" width="600" height="44"/>
4536
<autoresizingMask key="autoresizingMask"/>
4637
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="WCw-Qf-5nD" id="37f-cq-3Eg">
4738
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
@@ -76,7 +67,7 @@
7667
<outlet property="delegate" destination="7bK-jq-Zjz" id="RA6-mI-bju"/>
7768
</connections>
7869
</tableView>
79-
<navigationItem key="navigationItem" title="Swift Push" id="Zdf-7t-Un8"/>
70+
<navigationItem key="navigationItem" title="Swift Push" id="hbG-7c-IhC"/>
8071
<connections>
8172
<outlet property="searchDisplayController" destination="dxw-He-NIa" id="oCl-pX-9er"/>
8273
</connections>
@@ -93,7 +84,6 @@
9384
<searchDisplayController id="dxw-He-NIa">
9485
<connections>
9586
<outlet property="delegate" destination="7bK-jq-Zjz" id="vbG-y6-3ZK"/>
96-
<outlet property="searchBar" destination="Q08-01-f5b" id="WKA-yO-YDY"/>
9787
<outlet property="searchContentsController" destination="7bK-jq-Zjz" id="s5x-aM-s8f"/>
9888
<outlet property="searchResultsDataSource" destination="7bK-jq-Zjz" id="FTk-zh-qN3"/>
9989
<outlet property="searchResultsDelegate" destination="7bK-jq-Zjz" id="r0K-OC-dt5"/>

Swift Push 4/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>543</string>
22+
<string>553</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UIBackgroundModes</key>

Swift Push 4/MasterViewController.swift

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
import 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

Swift Push 4/Settings.bundle/Root.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<array>
77
<dict>
88
<key>DefaultValue</key>
9-
<string>543</string>
9+
<string>553</string>
1010
<key>Key</key>
1111
<string>CurrentBuildNumber</string>
1212
<key>Title</key>

0 commit comments

Comments
 (0)