UITableView中具有多个部分的Search Controller [英] Search Controller with multiple section in UITableView
问题描述
我有一个UIViewController,其中有一个UITableView,并且在该tableview中,我有多个节,其中包含一些项目,我必须在该tableview中使用itemname搜索.我已经在我的视图控制器中声明了这一点
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()
}
并且我具有SearchResultUpdate的扩展名
And I have an extension of SearchResultUpdate
extension NearByShopVC: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filteredshops(searchController.searchBar.text!)
}
}
我已经提到了这个问题,但是没有找到任何解决方法如何在'updateSearchResultsForSearchController'中过滤字典数组以使用Swift搜索UITableView
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!!
推荐答案
您可以使用的一种方法是过滤单个部分的内容,因此首先要像这样更改filteredshops
.
One way for you to is when you filter work with single section, so first change your filteredshops
like this way.
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方法可以像这样进行更改.
Now with tableView method make change like this way.
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
}
这篇关于UITableView中具有多个部分的Search Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!