Skip to content

Instantly share code, notes, and snippets.

@wickdninja
Created July 31, 2015 23:38
Show Gist options
  • Save wickdninja/73d09f8b544c11921689 to your computer and use it in GitHub Desktop.
Save wickdninja/73d09f8b544c11921689 to your computer and use it in GitHub Desktop.
Swift Search Snippets
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