swift - 在结构上使用 UISearchbar
问题描述
我的项目中有两个UITableViewControllers
。第一个只是一个简单的tableView
,显示一个普通的array
. 第二个tableView
是由Struct
我UISearcBar
在第一个中实现的,ViewController
没有任何麻烦。我遵循了本指南这是我在第一个中用于搜索栏的代码viewController
:
let listArray = ["Item1", "Item2", "Item3", "Item4", "Item5"]
var searchData: [String]!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
searchBar.delegate = self
searchData = listArray
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = searchData[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchData = searchText.isEmpty ? listArray : listArray { (item: String) -> Bool in
return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
}
tableView.reloadData()
}
在第二个中ViewController
,我使用 aStruct
填充tableView
当我尝试实现 searchBar 时出现错误:
struct ObjectModel {
var objectProducer: String?
var objecType: String?
var unit: String?
var contentSize: Double?
var amount: Double?
var addedDate: String?
}
var objectList = [ObjectModel]()
//Search data
var filteredData = [ObjectModel]()
override func viewDidLoad() {
super.viewDidLoad()
//SearchData
searchBar.delegate = self
filteredData = objectList //
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell") as! ListCell
//the artist object
let object: ObjectModel
object = objectList[indexPath.row]
if activeSearch = false {
cell.objectNameLabel.text = "\(object.objectProducer ?? "Missing data"), \(object.objecType ?? "Missing data")"
} else {
cell.objectNameLabel.text = filteredData[indexPath.row] //Error: Cannot assign value of type 'ObjectModel' to type 'String?'
}
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = searchText.isEmpty ? objectList : objectList.filter { (item: String) -> Bool in //Error: 'String' is not convertible to 'ObjectModel'
return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
}
tableView.reloadData()
}
使用时应该如何实现搜索Struct
?
解决方案
你必须改变
cell.objectNameLabel.text = filteredData[indexPath.row]
至
cell.objectNameLabel.text = filteredData[indexPath.row].objectProducer
因为在ObjectModel
struct 中,您必须分配必须分配的属性text
之一UILabel
。你可以分配
var objectProducer: String?
var objecType: String?
var unit: String?
var contentSize: Double?
var amount: Double?
var addedDate: String?
属性String
说明。例如,如果您想分配金额,您可以使用
cell.objectNameLabel.text = "\(filteredData[indexPath.row].amount!)"
或者有更好的方法。
您必须扩展您的ObjectModel
withCustomStringConvertible
以便您的所有结构变量都可以描述为字符串,然后它可以像那样工作
cell.objectNameLabel.text = filteredData[indexPath.row]
你可以像下面这样扩展它。
extension ObjectModel: CustomStringConvertible{
public var description: String {
return "amount \(self.amount) producer \(self.producer) bla bla"
}
}
// 编辑
用主线程更改它。
tableView.reloadData()
至 :
DispatchQueue.main.async {
self.tableView.reloadData()
}
推荐阅读
- python-3.x - pandas.Series.append() 将新值添加为额外列,而不是将其附加到系列的末尾
- python - 如何制作切换按钮
- oracle - 我的 Unpivot 查询结果的列不是我想要的
- python - 如何注释Seaborn中不包括异常值的箱形图的最小值?
- apache-flink - 为事件时间间隔连接找到多个行时间字段
- html - 如何在元素之间添加垂直间距在移动视图上?
- python - 从数据中的两个变量中可视化特定信息
- r - 有什么方法可以让变量按 group_by 分组?
- git - 如何使用远程分支更新您当前的功能分支?
- prolog - 定义一个同时包含数字及其否定的谓词并枚举它们