Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nathanborror/203d546389f96fcd51fb to your computer and use it in GitHub Desktop.
Save nathanborror/203d546389f96fcd51fb to your computer and use it in GitHub Desktop.
Programatic example of UISearchDisplayController
//
// ViewController.swift
// Example
//
// Created by Nathan Borror on 8/28/14.
// Copyright (c) 2014 Nathan Borror. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate {
let kCellIdentifier = "Cell"
var searchController: UISearchDisplayController!
var tableView: UITableView!
var tableData: [String]? {
didSet {
self.tableView.reloadData()
}
}
var tableDataFiltered = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: CGRectZero, style: .Plain)
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: kCellIdentifier)
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
let searchBar = UISearchBar()
searchBar.delegate = self
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
searchController = UISearchDisplayController(searchBar: searchBar, contentsController: self)
searchController.searchResultsDataSource = self
searchController.searchResultsDelegate = self
searchController.searchResultsTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: kCellIdentifier)
tableData = ["Foo", "Bar", "Baz"]
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = self.view.bounds
}
// UITableViewDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
if tableView != self.tableView {
return self.tableDataFiltered.count
}
if let count = tableData?.count {
return count
}
return 0
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier)
var data: String
if tableView != self.tableView {
data = tableDataFiltered[indexPath.row]
} else {
data = tableData![indexPath.row]
}
cell.textLabel.text = data
return cell
}
// UITableViewDelegate
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
if let data = tableData?[indexPath.row] {
let viewController = DetailViewController(data: data)
self.navigationController.pushViewController(viewController, animated: true)
}
}
// UISearchBarDelegate
func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {
self.tableDataFiltered = self.tableData!.filter({ (data: String) -> Bool in
let match = data.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return match != nil
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment