Reputation: 69
In my app I have a search bar where people can add free text and get the results the meet the criteria
func search(searchClinics: [Clinica], searchArr: [String]) -> [Clinica] {
var searchArr = searchArr
// base case - no more searches - return clinics found
if searchArr.count == 0 {
return searchClinics
}
// iterative case - search clinic with next search term and pass results to next search
let foundClinics = searchClinics.filter { item in
(item.name.lowercased() as AnyObject).contains(searchArr[0]) ||
item.specialty1.lowercased().contains(searchArr[0]) ||
item.specialty2.lowercased().contains(searchArr[0])
}
// remove completed search and call next search
searchArr.remove(at: 0)
return search(searchClinics: foundClinics, searchArr: searchArr)
}
I also have a flag to identify if the searchBar is being used
var searching = false // Checks if searchBar was used
In case searchBar was used, it returns the filtered array data otherwise returns the full list. For example
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching {
let totalClinics = clinicsSearch.count
if totalClinics == 0 {
return 1
} else {
return totalClinics
}
} else {
let totalClinics = clinics.count
if totalClinics == 0 {
return 1
} else {
return totalClinics
}
}
}
I'm now willing to add another viewController where the user would be able to define specific filters (like State, City, Specialty among others). Once the user clicks apply, it would go back to the previous viewController with filtering data.
I'm in doubt with the best approach to apply this filter. At first I though about doing something like:
I've a lot of searching in stackoverflow but I found a lot of filtering/searchbar stuff but none related to a separate search/filter viewController. Would you recommend this approach or is there a better way to do it? One of my concerns here if with step 4... if I call a segue, wouldn't I be stacking views and consuming more memory?
Thanks
Upvotes: 0
Views: 563
Reputation: 5693
In my app user click filter button present Controller open
let vc = storyboard?.instantiateViewController(withIdentifier: "searchFilter") as! SearchFilter
vc.modalPresentationStyle = .overCurrentContext
vc.SearchCompletion = {(model,flag) in
if(flag){
self.serachArr.removeAllObjects()
for i in 0..<model.count{
self.serachArr.add(ListModel(data: model[i] as! NSDictionary))
}
self.ListTable.reloadData()
}
}
self.present(vc, animated: true, completion: nil)
User click apply button and pass data previous Viewcontroller
self.SearchCompletion(dataArr,true)
Upvotes: 0