我有一个UIViewController,其中我有一个UITableView,在tableview中我有多个部分有一些项目,我必须在tableview中搜索itemname。我已经在我的视图控制器中声明了这个
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()
}
我有SearchResultUpdate
的扩展名extension NearByShopVC: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filteredshops(searchController.searchBar.text!)
}
}
我已经提到了这个问题但未找到任何解决方案How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to search a UITableView with Swift
我试过没有上述问题中的部分,然后也没有显示出正确的结果。谢谢!!
答案 0 :(得分:2)
您可以选择使用单个部分过滤工作,因此请先按照这种方式更改filteredshops
。
func filteredshops(_ searchText: String, scope: String = "All") {
let filteredShopsA = itemA.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 = itemB.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 = itemC.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
tableView.reloadData()
}
现在使用tableView方法以这种方式进行更改。
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 {
//Access filteredShops array
}
else {
switch (indexPath.section) {
case 0:
//Access itemsA[indexPath.row]
case 1:
//Access itemsB[indexPath.row]
default:
//Access itemsC[indexPath.row]
}
}
return cell
}