ios - UISearchController 取错项目
问题描述
这是我的代码,我在搜索之前点击 + 它提供了正确的产品。但问题是在搜索之后,它给出的以前的产品不是正确的产品
搜索后 UISearchController 查找 self.tableView 搜索工作正常
表视图
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "MasterViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? MasterViewCell else {
fatalError("The dequeued cell is not an instance of OutletViewCell.")
}
// Fetches the appropriate meal for the data source layout.
var product = products[indexPath.row]
if isFiltering {
product = filteredProducts[indexPath.row]
} else {
product = products[indexPath.row]
}
cell.productName.text = product.productDescription
cell.availQty.text = "Avail. Qty:" + String(product.stock)
cell.productPrice.text = "Price: " + String(product.defaultSellPrice)
cell.addItem.tag = indexPath.row
cell.addItem.addTarget(self, action: #selector(buttonTapped(button:)), for: .touchUpInside)
orderDetails.forEach { detail in
// print(word.price)
if detail.key == product.id {
cell.AddedQty.text = "Qty :" + String(detail.value.qty)
}
}
return cell
}
这是buttonTapped函数
@objc func buttonTapped(button: UIButton) {
// print("Button pressed " + String(button.tag))
let product=products[button.tag]
print(product.productDescription)
showAlert(product: product)
}
解决方案
在您的 buttonTapped 函数中,您还需要检查数据是否已过滤
@objc func buttonTapped(button: UIButton) {
let productData = isFiltering ? filteredProducts[indexPath.row] : products[indexPath.row]
showAlert(product: productData)
}
推荐阅读
- vue.js - Vue:深层嵌套对象上的观察者会记录 oldVal 和 newVal 吗?
- math - TypeError:无法将复数转换为浮点数(Python3,求解代数方程)
- java - JSF 操作顺序,转换器应该在验证器之前应用吗?
- android - 您已在调试模式下发送了签名的 APK 或 Android App Bundle。在发布模式下签名。如何修复它(颤振)
- image-processing - 使用单应性扭曲图像的四个角并返回无效的四边形
- java - 如何替换多个字符串字符?
- deep-learning - 从封闭系统中的本地目录导入 MNIST 数据集
- python - 如何从单独的脚本访问文档字符串?
- blazor - 当 ASPNETCORE_ENVIRONMENT 更改为“开发”以外的任何内容时 - 在 VS 中调试时,Blazor 应用程序看不到来自 razor 类库的资源
- excel - 如何避免“选择”来优化以下代码