ios - 在 UITableView 中搜索具有多个部分的 Controller
<p><p>我有一个 UIViewController,其中有一个 UITableView,并且在该 tableview 中我有多个部分,其中包含一些项目,我必须在该 tableview 中使用 itemname 进行搜索。我已经在我的 ViewController 中声明了这一点</p>
<pre><code>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 =
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
cell.storeName.text = filter["itemName"]
}
switch (indexPath.section) {
case 0:
//Access itemsA
case 1:
//Access itemsB
default:
//Access itemsC
}
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()
}
</code></pre>
<p>我有一个 SearchResultUpdate 的扩展</p>
<pre><code>extension NearByShopVC: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filteredshops(searchController.searchBar.text!)
}
}
</code></pre>
<p>我引用了这个问题,但没有找到任何解决方案 <a href="https://stackoverflow.com/questions/37245342/how-can-i-filter-an-array-of-dictionaries-in-updatesearchresultsforsearchcontro" rel="noreferrer noopener nofollow">How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to search a UITableView with Swift</a> </p>
<p>我试过没有上述问题中的部分,然后也没有显示正确的结果。谢谢!!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>一种方法是当您过滤单个部分的工作时,因此首先像这样更改您的 <code>filteredshops</code>。</p>
<pre><code>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()
}
</code></pre>
<p>现在使用 tableView 方法进行更改。</p>
<pre><code>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
case 1:
//Access itemsB
default:
//Access itemsC
}
}
return cell
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 UITableView 中搜索具有多个部分的 Controller ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43609615/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43609615/
</a>
</p>
页:
[1]