【发布时间】:2019-10-01 11:11:16
【问题描述】:
我正在关注一个关于如何为我的 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 是我的表视图控制器的名称。在我的教程项目中唯一不同的是我有一个名为 Country 的数组,其中包含多行字典。我将在这里发布我的其他部分代码
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
}
}
【问题讨论】:
标签: arrays swift uitableview uisearchbar uisearchcontroller