有用到 searchResultsController
代碼: 選擇全部
import UIKit
class Model: NSObject
{
var Name: String
var section: Int?
init(name: String)
{
self.Name = name
}
}
代碼: 選擇全部
class SearchResultCollectionCell: UICollectionViewCell
{
@IBOutlet weak var image: UIImageView!
}
代碼: 選擇全部
class SearchResultCollectionItemViewController: UICollectionViewController
{
var DataSource: [Model] = [Model]()
override func viewDidLoad()
{
super.viewDidLoad()
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return DataSource.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: SearchResultCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! SearchResultCollectionCell
let m: Model = DataSource[indexPath.row]
cell.image.image = UIImage(named: m.Name)
return cell
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
代碼: 選擇全部
class ViewController: UITableViewController, UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating
{
var DataSource: [Model] = [Model]()
/// Search controller to help us with filtering.
var searchController: UISearchController!
/// Secondary search results table view.
var resultsTableController: SearchResultCollectionItemViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
DataSource.append(Model(name: "facebook"))
DataSource.append(Model(name: "insagram"))
DataSource.append(Model(name: "pinterest"))
DataSource.append(Model(name: "tumblr"))
DataSource.append(Model(name: "twitter"))
resultsTableController = self.storyboard!.instantiateViewControllerWithIdentifier("SearchResultIdentity") as! SearchResultCollectionItemViewController
resultsTableController.DataSource = DataSource
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchBar.placeholder = "Search for Mame"
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
//searchController.hidesNavigationBarDuringPresentation = false
self.tableView.tableHeaderView = searchController.searchBar
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false // default is YES
searchController.searchBar.delegate = self // so we can monitor text changes + others
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return DataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let cust = self.DataSource[indexPath.row]
cell.textLabel?.text = cust.Name
return cell
}
// MARK: UISearchBarDelegate
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
}
// MARK: UISearchControllerDelegate
func presentSearchController(searchController: UISearchController) {
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).")
}
func willPresentSearchController(searchController: UISearchController) {
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).")
}
func didPresentSearchController(searchController: UISearchController) {
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).")
}
func willDismissSearchController(searchController: UISearchController) {
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).")
}
func didDismissSearchController(searchController: UISearchController) {
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).")
}
// MARK: UISearchResultsUpdating
func updateSearchResultsForSearchController(searchController: UISearchController)
{
var filtered: [Model] = [Model]()
filtered = DataSource.filter({ (string) -> Bool in
if let searchTerm = self.searchController?.searchBar.text {
let searchTermMatches = searchString(searchTerm, searchTarget: "\(string.Name)").count > 0
if searchTermMatches {
return true
}
}
return false
})
let resultsController = searchController.searchResultsController as! SearchResultCollectionItemViewController
resultsController.DataSource = filtered.count == 0 ? DataSource : filtered
resultsController.collectionView!.reloadData()
}
func searchString(searchTerm:String, searchTarget:String) -> Array<AnyObject>
{
var matches:Array<AnyObject> = []
do {
let regex = try NSRegularExpression(pattern: searchTerm, options: [.CaseInsensitive, .AllowCommentsAndWhitespace])
let range = NSMakeRange(0, searchTarget.characters.count)
matches = regex.matchesInString(searchTarget, options: [], range: range)
} catch _ {
}
return matches
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}