add search bar

This commit is contained in:
Bob Sun 2017-01-23 12:48:20 +08:00
parent 9effb961d2
commit 2099d52923
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
2 changed files with 63 additions and 12 deletions

View file

@ -49,10 +49,21 @@
<navigationItem key="navigationItem" title="All" id="2I8-8c-JEA">
<barButtonItem key="rightBarButtonItem" systemItem="refresh" id="JLS-BH-aaR"/>
</navigationItem>
<connections>
<outlet property="searchDisplayController" destination="pdj-8n-ZyE" id="ZtO-ar-qbo"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="CHd-Vz-2cA" userLabel="First Responder" sceneMemberID="firstResponder"/>
<searchDisplayController id="pdj-8n-ZyE">
<connections>
<outlet property="delegate" destination="vGH-8E-Zez" id="Fkb-Ib-yTT"/>
<outlet property="searchContentsController" destination="vGH-8E-Zez" id="9RT-Uy-M4k"/>
<outlet property="searchResultsDataSource" destination="vGH-8E-Zez" id="ya0-IV-wks"/>
<outlet property="searchResultsDelegate" destination="vGH-8E-Zez" id="2Vl-Dg-zmv"/>
</connections>
</searchDisplayController>
</objects>
<point key="canvasLocation" x="2425" y="-1"/>
<point key="canvasLocation" x="2425" y="469"/>
</scene>
<!--Settings-->
<scene sceneID="d9R-gZ-ywG">
@ -151,7 +162,7 @@
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="leR-cc-QPW" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="2583" y="2424"/>
<point key="canvasLocation" x="2425" y="2424"/>
</scene>
<!--Git Server-->
<scene sceneID="Obl-ql-ILG">
@ -280,7 +291,7 @@
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="https://" minimumFontSize="17" clearButtonMode="unlessEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Rb8-zs-TGa">
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="https://" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Rb8-zs-TGa">
<rect key="frame" x="15" y="22.5" width="352" height="21"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
@ -309,7 +320,7 @@
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="this_is_passphrase" minimumFontSize="17" clearButtonMode="unlessEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Nkx-gN-jNA">
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="this_is_passphrase" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Nkx-gN-jNA">
<rect key="frame" x="15" y="22.5" width="352" height="21"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
@ -353,7 +364,7 @@
<placeholder placeholderIdentifier="IBFirstResponder" id="hbx-RC-qg1" userLabel="First Responder" sceneMemberID="firstResponder"/>
<exit id="jjl-Xi-fkn" userLabel="Exit" sceneMemberID="exit"/>
</objects>
<point key="canvasLocation" x="4450" y="3304"/>
<point key="canvasLocation" x="4449" y="3236"/>
</scene>
<!--Password Detail View Controller-->
<scene sceneID="9b5-fu-vx9">
@ -389,7 +400,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="ek1-Cw-dZH" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="3272.8000000000002" y="-1.3493253373313345"/>
<point key="canvasLocation" x="3273" y="469"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="ACd-rk-Zf3">
@ -408,7 +419,7 @@
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="25X-hm-lLo" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1486" y="-1"/>
<point key="canvasLocation" x="1486" y="469"/>
</scene>
<!--Tab Bar Controller-->
<scene sceneID="QAo-AP-q8o">
@ -427,7 +438,7 @@
</tabBarController>
<placeholder placeholderIdentifier="IBFirstResponder" id="h18-hY-pkk" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="215" y="1137"/>
<point key="canvasLocation" x="-322" y="1916"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="sdr-gl-b19">
@ -482,7 +493,7 @@
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="bVY-G2-9Ud" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="3431" y="3304"/>
<point key="canvasLocation" x="3430" y="3236"/>
</scene>
</scenes>
</document>

View file

@ -11,13 +11,38 @@ import SwiftGit2
import Result
import SVProgressHUD
extension PasswordTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
class PasswordTableViewController: UITableViewController {
private var passwordEntities: [PasswordEntity]?
var filteredPasswordEntities = [PasswordEntity]()
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
NotificationCenter.default.addObserver(self, selector: #selector(PasswordTableViewController.actOnPasswordUpdatedNotification), name: NSNotification.Name(rawValue: "passwordUpdated"), object: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false)
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredPasswordEntities = passwordEntities!.filter { password in
return password.name!.lowercased().contains(searchText.lowercased())
}
tableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
func actOnPasswordUpdatedNotification() {
@ -27,20 +52,35 @@ class PasswordTableViewController: UITableViewController {
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive && searchController.searchBar.text != "" {
return filteredPasswordEntities.count
}
return passwordEntities!.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath)
cell.textLabel?.text = passwordEntities![indexPath.row].name
let password: PasswordEntity
if searchController.isActive && searchController.searchBar.text != "" {
password = filteredPasswordEntities[indexPath.row]
} else {
password = passwordEntities![indexPath.row]
}
cell.textLabel?.text = password.name
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showPasswordDetail" {
if let viewController = segue.destination as? PasswordDetailViewController {
let selectedRow = tableView.indexPathForSelectedRow!.row
viewController.passwordEntity = passwordEntities![selectedRow]
let selectedIndex = self.tableView.indexPath(for: sender as! UITableViewCell)!
let password: PasswordEntity
if searchController.isActive && searchController.searchBar.text != "" {
password = filteredPasswordEntities[selectedIndex.row]
} else {
password = passwordEntities![selectedIndex.row]
}
viewController.passwordEntity = password
}
}
}