Skip to content

Instantly share code, notes, and snippets.

@pavangandhi
Created February 8, 2017 04:56
Show Gist options
  • Save pavangandhi/4d7ac6b1a2803b692a7fffd770fdc841 to your computer and use it in GitHub Desktop.
Save pavangandhi/4d7ac6b1a2803b692a7fffd770fdc841 to your computer and use it in GitHub Desktop.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.isEmpty
{
search = String(search.characters.dropLast())
}
else
{
search=textField.text!+string
}
print(search)
let predicate=NSPredicate(format: "SELF.itemTitle CONTAINS[cd] %@", search)
filtered = (data as [itemClass] as NSArray).filtered(using: predicate) as! [itemClass]
//let searchPredicate = NSPredicate(format: "itemTitle CONTAINS[cd] %@", textField.text!)
// filtered = data.filter({ (text) -> Bool in
// let tmp: NSString = (text as itemClass).itemTitle as NSString
// let range = tmp.range(of: string, options: NSString.CompareOptions.caseInsensitive)
// return range.location != NSNotFound
// })
if(filtered.count == 0){
searchActive = false;
self.tableViewitemsData.isHidden = true
self.lblNoRecords.isHidden = false
} else {
searchActive = true
self.tableViewitemsData.isHidden = false
self.lblNoRecords.isHidden = true
}
self.tableViewitemsData.reloadData()
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment