将 SearchBar 添加到 uitableview 在过滤过程中遇到错误
Add A SearchBar to a uitableview confronts an error in filter process
我正在学习如何将 ui table 视图的搜索栏添加到我的 swift 项目中,我遵循了这个 link,https://www.youtube.com/watch?v=XtiamBbL5QU 并且我卡在了代码的一半。在我项目的这一行中
self.countries.filter { (Country:String) -> Bool in
<#code#>
}
我有这个错误
String' is not convertible to 'HistoryViewController.Country
HistoryViewController 是我的 table 视图控制器的名称。在我的教程项目中,唯一不同的是我有一个名为 Countries 的数组,其中包含多行字典。我要 post 我的其他部分代码在这里
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 { [=12=].countryName == "Pakistan" }
编辑
要获取国家/地区列表 names
(即 [String]
),您必须对过滤结果使用 map
,如下所示,
let filteredCountryNames = self.countries.filter { (country: Country) -> Bool in
return country.countryName == "Pakistan"
}.map { [=13=].countryName }
您没有字典,而是一组国家/地区,因此您的过滤器闭包应如下所示
let result = countries.filter { [=10=].countryName.starts(with: searchStr) }
或
let result = countries.filter { [=11=].countryName == searchStr) }
取决于您要如何过滤。如果你想让搜索不区分大小写,那么为 属性 和搜索字符串
调用 lowercased()
let result = countries.filter { [=12=].countryName.lowercased().starts(with: searchStr.lowercased()) }
我正在学习如何将 ui table 视图的搜索栏添加到我的 swift 项目中,我遵循了这个 link,https://www.youtube.com/watch?v=XtiamBbL5QU 并且我卡在了代码的一半。在我项目的这一行中
self.countries.filter { (Country:String) -> Bool in
<#code#>
}
我有这个错误
String' is not convertible to 'HistoryViewController.Country
HistoryViewController 是我的 table 视图控制器的名称。在我的教程项目中,唯一不同的是我有一个名为 Countries 的数组,其中包含多行字典。我要 post 我的其他部分代码在这里
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 { [=12=].countryName == "Pakistan" }
编辑
要获取国家/地区列表 names
(即 [String]
),您必须对过滤结果使用 map
,如下所示,
let filteredCountryNames = self.countries.filter { (country: Country) -> Bool in
return country.countryName == "Pakistan"
}.map { [=13=].countryName }
您没有字典,而是一组国家/地区,因此您的过滤器闭包应如下所示
let result = countries.filter { [=10=].countryName.starts(with: searchStr) }
或
let result = countries.filter { [=11=].countryName == searchStr) }
取决于您要如何过滤。如果你想让搜索不区分大小写,那么为 属性 和搜索字符串
调用lowercased()
let result = countries.filter { [=12=].countryName.lowercased().starts(with: searchStr.lowercased()) }