首页 > 解决方案 > 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)

}

标签: iosswift

解决方案


在您的 buttonTapped 函数中,您还需要检查数据是否已过滤

  @objc func buttonTapped(button: UIButton) {
    let productData = isFiltering ? filteredProducts[indexPath.row] : products[indexPath.row]
    showAlert(product: productData)
}

推荐阅读