向uitableview中添加SearchBar会遇到筛选过程中的错误

时间:2019-10-01 11:11:16

标签: arrays swift uitableview uisearchbar uisearchcontroller

我正在跟踪有关如何向我的swift项目中添加ui表视图搜索栏的教程,我点击了此链接,https://www.youtube.com/watch?v=XtiamBbL5QU,并且陷入了代码的一半。在我的项目的这一行

 self.countries.filter { (Country:String) -> Bool in
            <#code#>
        }

我遇到此错误 String' is not convertible to 'HistoryViewController.Country HistoryViewController是我的表视图控制器的名称。在我的教程项目中,唯一不同的是我有一个名为Countrys的数组,其中包含几行词典。我将在此处发布其他代码部分

import UIKit

class HistoryViewController: UITableViewController , UISearchResultsUpdating {


    var searchController : UISearchController!
    var resultsController = UITableViewController()


    var myPlistArray: [String] = []

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return 115 //or whatever you need
    }

    struct Country : Decodable {
        let flagEmoji, Abb, countryName, pName : String

        private enum CointryKeys : String, CodingKey { case flagEmoji, Abb, countryName, pName }
    }

    var countries = [Country]()

    func updateSearchResults(for searchController: UISearchController) {
        //Filter through currencies

        self.countries.filter { (Country:String) -> Bool in
            <#code#>
        }

        // Update the results Table View

    }




    override func viewDidLoad() {
        super.viewDidLoad()


        self.searchController = UISearchController(searchResultsController: self.resultsController)
        self.tableView.tableHeaderView = self.searchController.searchBar
        self.searchController.searchResultsUpdater = self

        let url = Bundle.main.url(forResource: "Curr", withExtension: "plist")!
        let data = try! Data(contentsOf: url)


        do {

            countries = try PropertyListDecoder().decode([Country].self, from: data)
        } catch {
            // Handle error
            print(error)
        }
        print(countries)
        print("countries.count:", countries.count)


    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return countries.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("hiiii")

        let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell", for: indexPath) as! TableViewCellforHistory

        // Configure the cell...


        cell.lblCellHistory.text = countries[indexPath.row].countryName
        cell.lblEmoji.text = countries[indexPath.row].flagEmoji
        cell.lblCurrencyName.text = countries[indexPath.row].pName

        return cell
    }
 }

2 个答案:

答案 0 :(得分:1)

如下更改filter闭包,因为countriesCountry的数组,但是您将其视为String(通过执行Country: String),< / p>

 self.countries.filter { country -> Bool in
      return country.countryName == "Pakistan"
 }

 self.countries.filter { (country: Country) -> Bool in
      return country.countryName == "Pakistan"
 }

或者简单地

self.countries.filter { $0.countryName == "Pakistan" }

修改

要获取国家/地区names(即[String])的列表,您必须对以下过滤结果使用map

let filteredCountryNames = self.countries.filter { (country: Country) -> Bool in
          return country.countryName == "Pakistan"
     }.map { $0.countryName }

答案 1 :(得分:1)

您没有字典,而是有多个国家/地区,因此您的过滤器关闭应如下所示

let result  = countries.filter { $0.countryName.starts(with: searchStr) }

let result  = countries.filter { $0.countryName == searchStr) }

取决于您要如何过滤。如果要使搜索不区分大小写,请同时为属性和搜索字符串调用lowercased()

let result  = countries.filter { $0.countryName.lowercased().starts(with: searchStr.lowercased()) }