gpt4 book ai didi

arrays - 将 SearchBar 添加到 uitableview 在过滤过程中遇到错误

转载 作者:搜寻专家 更新时间:2023-11-01 06:49:59 25 4
gpt4 key购买 nike

我正在学习如何将 ui TableView 的搜索栏添加到我的 swift 项目中,我点击了此链接,https://www.youtube.com/watch?v=XtiamBbL5QU我被困在代码的一半。在我项目的这一行中

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

我有这个错误String' 不可转换为 'HistoryViewController.CountryHistoryViewController 是我的 TableView Controller 的名称。在我的教程项目中,唯一不同的是我有一个名为 Countries 的数组,其中包含多行字典。我将在这里发布我的其他部分代码

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
}
}

最佳答案

如下更改 filter 闭包,因为 countries 是一个 Country 数组,但您将其视为 String(通过执行 Country: String),

 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 }

关于arrays - 将 SearchBar 添加到 uitableview 在过滤过程中遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58183615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com