OStack程序员社区-中国程序员成长平台

标题: ios - 在 UITableView 中搜索具有多个部分的 Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 01:21
标题: ios - 在 UITableView 中搜索具有多个部分的 Controller

我有一个 UIViewController,其中有一个 UITableView,并且在该 tableview 中我有多个部分,其中包含一些项目,我必须在该 tableview 中使用 itemname 进行搜索。我已经在我的 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 的扩展

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

我试过没有上述问题中的部分,然后也没有显示正确的结果。谢谢!!



Best Answer-推荐答案


一种方法是当您过滤单个部分的工作时,因此首先像这样更改您的 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
}

关于ios - 在 UITableView 中搜索具有多个部分的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609615/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4