Created
July 31, 2015 23:38
-
-
Save wickdninja/73d09f8b544c11921689 to your computer and use it in GitHub Desktop.
Swift Search Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var resultSearchController = UISearchController() | |
self.resultSearchController = ({ | |
let controller = UISearchController(searchResultsController: nil) | |
controller.dimsBackgroundDuringPresentation = false | |
controller.hidesNavigationBarDuringPresentation = true | |
controller.searchBar.sizeToFit() | |
self.navigationController?.view = controller.searchBar | |
self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(controller.searchBar.frame)); | |
return controller | |
})() | |
self.resultSearchController = ({ | |
let controller = UISearchController(searchResultsController: nil) | |
controller.dimsBackgroundDuringPresentation = false | |
controller.hidesNavigationBarDuringPresentation = true | |
controller.searchBar.sizeToFit() | |
self.navigationController?.view = controller.searchBar | |
self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(controller.searchBar.frame)); | |
return controller | |
})() | |
/* Search */ | |
func updateSearchResultsForSearchController(searchController: UISearchController) | |
{ | |
filteredTableData = tableData | |
var searchTerms = searchController.searchBar.text | |
if(searchTerms.length > 0){ | |
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text) | |
filteredTableData = tableData.filter{searchPredicate.evaluateWithObject($0.name)} | |
} | |
self.tableView.reloadData() | |
} | |
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier("templateCell", forIndexPath: indexPath) as! TemplateCell | |
var template:PaymentTemplate! | |
if (self.resultSearchController.active) { | |
template = filteredTableData[indexPath.section] | |
}else{ | |
template = tableData[indexPath.section] | |
} | |
//configure cell | |
return cell | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment