Swift Uitable View单元搜索不起作用



我的场景,我正在尝试为我的UITAITHVIEW实现搜索选项。在这里,我在UitableView中使用自定义单元格。在我的数组数据中,我还添加了部分。如何解决这个问题?

我的代码下面的代码

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating, UISearchBarDelegate {
          var allDetails:[(String,[String])] = []
          var searchResult:[(String,[String])] = []
          @IBOutlet weak var tableVIew: UITableView!
          @IBOutlet weak var searchBar: UISearchBar!
        override func viewDidLoad() {
            super.viewDidLoad()
            allDetails = [("Vadavalli",["Vadvalli", "Mullai Nagar", "P.N.Pudhur"]),
                              ("R.S.Puram",["Lawly Road", "Kowly Brown", "D.B Road"]),
                              ("Town Hall",["Raja Street", "Gandhipark", "Five corner road", "Main Town Hall"])]
            searchResult = allDetails    
          }
        func numberOfSections(in tableView: UITableView) -> Int {
            return searchResult.count
          }
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return searchResult[section].1.count
          }
          func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return searchResult[section].0
          }
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
            if cell == nil{
              cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
            }
            cell?.textLabel?.text = searchResult[indexPath.section].1[indexPath.row]
            return cell!
          }
          func updateSearchResults(for searchController: UISearchController) { // SEARCH NOT WORKING
             if let searchText = searchController.searchBar.text {
                    searchResult = allDetails.map({ (areaTitle:$0.0,areas:$0.1.filter({ $0.lowercased().contains(searchText) })) }).filter { !$0.1.isEmpty}
                    //self.tableVIew.reloadData();
                }
            }
}

您需要做的是实现UISearchBarDelegate方法searchBar(_:textDidChange:)。每当您的搜索栏文本更改时,这都会调用。在此方法中,我们可以过滤allDetails并重新加载我们的表观视图。

我们将您的updateSearchResults(for searchController:)方法中的所有代码从本质上移动到searchBar(_:textDidChange:)委托方法。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating, UISearchBarDelegate {
    var allDetails:[(String,[String])] = []
    var searchResult:[(String,[String])] = []
    @IBOutlet weak var tableVIew: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!
    override func viewDidLoad() {
        super.viewDidLoad()
        //New line of code! Be sure to set your search bar's delegate in viewDidLoad()
        searchBar.delegate = self
        allDetails = [("Vadavalli",["Vadvalli", "Mullai Nagar", "P.N.Pudhur"]),
                  ("R.S.Puram",["Lawly Road", "Kowly Brown", "D.B Road"]),
                  ("Town Hall",["Raja Street", "Gandhipark", "Five corner road", "Main Town Hall"])]
        searchResult = allDetails
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return searchResult.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResult[section].1.count
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return searchResult[section].0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
        }
        cell?.textLabel?.text = searchResult[indexPath.section].1[indexPath.row]
        return cell!
    }
    //Let's get rid of this function
    /*
    func updateSearchResults(for searchController: UISearchController) {
        // SEARCH NOT WORKING
        if let searchText = searchController.searchBar.text {
            searchResult = allDetails.map({ (areaTitle:$0.0,areas:$0.1.filter({ $0.lowercased().contains(searchText) })) }).filter { !$0.1.isEmpty }
            //self.tableVIew.reloadData();
        }
    }*/
    //And implement the UISearchBarDelegate method. This method fires every time that the search bar's text changes
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchResult = allDetails.map({ (areaTitle:$0.0,areas:$0.1.filter({ $0.lowercased().contains(searchText) })) }).filter { !$0.1.isEmpty }
        self.tableView.reloadData();
    }
}

最新更新