【发布时间】:2019-06-19 11:29:08
【问题描述】:
您好,我使用array 模型数据,我将该数据加载到tableview。
但我想根据textField 中的用户输入搜索该数据。
- 代码。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let model = array[indexPath.row]
if let rate = model.price
{
cell.pricelbl.text = "$" + rate
}
cell.namelbl.text = model.prodName ?? ""
}
//Textfield delegate methods.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == searchtextfield
{
var foundItems = [BaseApparel]()
foundItems = self.array.filter { $0.prodName == searchtextfield.text! }
self.array = foundItems
self.searchtableview.reloadData()
}
return true
}
【问题讨论】:
-
有什么问题
-
当我尝试在文本字段中输入文本时,它不会在 tableview 中显示任何数据。
-
设置委托和数据源
-
你用搜索栏搜索了吗
-
不,我使用文本字段
标签: ios arrays swift dictionary tableview