Reputation: 288
So, I have a tableview , a textfield, and an array which contains some data. What i want to achieve is that whenever I start typing in the textfield, a filter() method should fire on my array containing the data and the tablview should return data matching to the characters I typed in the textfield.
For eg, if i type W it should give me result as "Walmart","Wally,"Wily" and so on. IF i type in "Sac" it should give "sacramento","satisfied" and so on. In short, an auto-complete textfield without using any external libraries at all.
I know this is simple , i have tried doing it but i am getting zero data.Also, i am new to swift. Please Help anybody :-)
Upvotes: 0
Views: 1935
Reputation: 288
LOl I am a noob and yet i m answering my own question. Here arrStoreNameFilteredData and arrStoreName are both arrays containing data of same object type. However, arrStoreNameFilteredData will contain the data that will be filtered out whenever the user will start typing a character in the textfield.
if textField.text?.isEmpty == true && string.isEmpty == true
{
textField.text = ""
arrStoreNameFilteredData = arrStoreName
tblData.reloadData()
}
else
{
tblData.isHidden = false
let substring = (textField.text! as NSString).replacingCharacters(in: range, with: string)
print("Substring : \(substring)")
arrStoreNameFilteredData = arrStoreName.filter({ (store: Stores) -> Bool in
return ((store.storeName?.lowercased())?.contains(substring.lowercased()))!
})
tblData.reloadData()
}
the filter predicate doesn't work quite as good when you use it with "$0"..instead use an alias variable. Here, store is aa variable of object type Stores, which will then filter out the data as i type in each character.
((store.storeName?.lowercased())?.contains(substring.lowercased()))!
The above line is what made it work for me :-D. Thanks to all the people who replied to my post tho, without them i wouldn't have been able to post my own answer :-D.
Upvotes: 0
Reputation: 4080
Look at this link
for my case, Here is a solution with UISearchbar.I am getting an array of dictionary & perform the search
//MARK:- search items & reload table
func filter(array : [[String : [MyModel]]], byString filterString : String) {
let searchByNamesArray = array.filter { $0.values.contains { $0.contains { $0.nameString.lowercased().contains(filterString.lowercased())} } }
//Assign searched array into arrayOfMerchants
self.arrayOfItems = searchByNamesArray
//Reload table with new Data
self.merchantTableView.reloadData()
}
For Search bar code
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
if !((searchBar.text?.isEmpty)!){
filter(array: self.arrayOfItems, byString: searchBar.text!)
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if (searchBar.text?.isEmpty)! //in case there is no text in Searchbar, reload all record
{
self.arrayOfItems = self.arrayOfRecordsHolding //array of original record holder
self.merchantTableView.reloadData()
}
else
{
self.arrayOfItems = self.arrayOfRecordsHolding
filter(array: self.arrayOfMerchants, byString: searchBar.text!)
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
//reload all data when cancel button pressed on searchbar
self.arrayOfItems = self.arrayOfRecordsHolding
searchBar.text = ""
self.merchantTableView.reloadData()
searchBar.resignFirstResponder()
}
Upvotes: 2