I have a UIViewController in which I have a UITableView and inside that tableview I have multiple sections which have some item and I have to search with itemname inside that tableview. I have declared this in my view controller
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = UIColor(red: 39.0/255.0, green: 203.0/255.0, blue: 192.0/255.0, alpha: 1.0)
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.placeholder = "Search item"
var sections = [Category A, Category B, Category C]
var itemsA = [["Item": "item A","ItemId" : "1"],["Item": "itemB","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
var itemsB = [["Item": "item A","ItemId" : "1"],["Item": "itemB","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
var itemsC = [["Item": "item A","ItemId" : "1"],["Item": "itemB","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
}
func numberOfSections(in tableView: UITableView) -> Int {
if searchController.isActive {
return 1
}
return self.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive {
return filteredShops.count
}
switch (section) {
case 0:
return itemsA.count
case 1:
return itemsB.count
default:
return itemsC.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StoreCell") as! UITableViewCell
if searchController.isActive {
let filter = filteredShops[indexPath.row]
cell.storeName.text = filter["itemName"]
}
switch (indexPath.section) {
case 0:
//Access itemsA[indexPath.row]
case 1:
//Access itemsB[indexPath.row]
default:
//Access itemsC[indexPath.row]
}
return cell
}
func filteredshops(_ searchText: String, scope: String = "All") {
let filteredShopsA = storeLists.filter({ item in
if let name = item["itemName"], let query = searchController.searchBar.text {
return name.range(of: query, options: [.caseInsensitive, .diacriticInsensitive]) != nil
}
return false
})
let filteredShopsB = medicalLists.filter({ item in
if let name = item["itemName"], let query = searchController.searchBar.text {
return name.range(of: query, options: [.caseInsensitive, .diacriticInsensitive]) != nil
}
return false
})
let filteredShopsC = restaurantsLists.filter({ item in
if let name = item["itemName"], let query = searchController.searchBar.text {
return name.range(of: query, options: [.caseInsensitive, .diacriticInsensitive]) != nil
}
return false
})
filteredShops = filteredShopsA + filteredShopsB + filteredShopsC
print(filteredShops)
tableView.reloadData()
}
And I have an extension of SearchResultUpdate
extension NearByShopVC: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filteredshops(searchController.searchBar.text!)
}
}
I had refer this question but not found any solution How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to search a UITableView with Swift
I tried without the section in the above said question then also not showing proper results. Thank you!!