当我们在 DidSelect 上附加数组时,我们必须这样做以删除 DidDeselect 中的确切对象

问题描述

我有 productArrayAny = [String: Any] 和 productDicAny = String:Any 我在从 api 获取数据时附加了 productarray 以确保全选,我需要按照 product.pk 附加数组并删除也与我尝试使用 firstIndex 进行 diddeselect 相同,但它没有选择删除确切的产品。

// 从 api 获取

 for proJSON in proArray {
                                            //   let product = wishProducts.parseProduct(proJSON: proJSON)
                                            let product = WishProducts.parseProduct(proJSON: proJSON)
                                            self.wishProducts.append(product)

                                            // extracting product array to fill the select all items
                                            self.productDicAny["product"] = product.pk
                                            switch product.uom {
                                                case "kg":
                                                    self.para["quantity"] = product.damount
                                                default:
                                                    self.para["quantity"] = product.amount
                                            }

//didselect tableview

let product = self.wishProducts[indexPath.row]
        self.selectedProductID = product.pk
        self.selectdQty = product.amount
self.productDicAny["product"] = product.pk
                switch product.uom {
                    case "kg":
                        self.productDicAny["quantity"] = product.damount
                    default:
                        self.productDicAny["quantity"] = product.amount
                }
                                                
                self.productArrayAny.append(self.productDicAny)

取消选择时如何删除确切的 product.pk 项目。 我尝试遵循所有方法,但在取消选择时并没有删除确切的产品 ID

// diddeselect

 let deselectIndexPath = indexPath.row
           //   print("deselected Index Path: \(deselectIndexPath)")
            //  self.para.removeObject(forKey: "product")
            //  self.para.removeObject(forKey: "quantity")
             productArrayAny.remove(at: deselectIndexPath)
              //  let index = productArrayAny.firstIndex(of: "product": "\(String(DeslectedProductID ?? 0))")
           
              //  productArray.remove(at: index ?? 0)
           //  productArray = productArray.filter { $0 != ["product": "\(String(self.DeslectedProductID ?? 0))"] }
             //   print("Product Dictionary diddeselect : \(self.productArray)  || \(self.para)")
             //   productArray.remove(at: index ?? 0)
               // self.productDicAny.removeValue(forKey: "product")
               // self.productDicAny.removeValue(forKey: "quantity")
               // self.productArrayAny.append(self.productDicAny)

发布时我的字典回复

[["product": 6209],["product": 8672],["product": 9101]]

解决方法

试试这样:

productArrayAny.beginUpdates()
productArrayAny.remove(at: deselectIndexPath)       
productArrayAny.endUpdates()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...