Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

ios - How to remove dictionary key and values when Deselecting and my selecting and deselecting methods are not efficient needs to be correct also

I wanted to add some shoppinglist products as shown below shoppingList I have a Dictionary that will hold a selected products at the beginning I am fetching that from cellForRow method as below //variable declaration

var productArray = [[String: String]]()
var productIDString: String?
var productQtyString: String?
let para:NSMutableDictionary = NSMutableDictionary()

  let stringProID = String(product.pk ?? 0)
            let stringProQty = proCell.qtyCountLabel.text
            self.para.setValue(stringProID, forKey: "product")
            self.para.setValue(stringProQty, forKey: "quantity")
            self.productArray.append(self.para as! [String : String])

So this will fill the array as selected as below

response at cell for-row : [["product": "26259", "quantity": "0.20"], ["quantity": "0.40", "product": "25973"], ["product": "15522", "quantity": "1"]]

I don't know how to select all items by default so I filled like this and apply selected state for UI. Please some body post some reference codes to follow

Then for the didSelect part I did as following

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 1 {
    
    let cell:ShoppingListProductsCell = tableView.cellForRow(at: indexPath) as! ShoppingListProductsCell
         
        cell.selectedState()

    let product = self.wishProducts[indexPath.row]
    self.selectedProductID = product.pk
    self.selectdQty = product.amount
    self.selectedIDArray.append(self.selectedProductID ?? 0)
    print("Selected ID Array (self.selectedIDArray)")
    self.selectedQtyArray.append(self.selectdQty ?? 0)
        print("selected qty Array (self.selectedQtyArray)")
        self.productIDString = String(self.selectedProductID ?? 0)
        self.productQtyString = String(self.selectdQty ?? 0 )
        self.para.setValue(self.productIDString, forKey: "product")
        self.para.setValue(self.productQtyString, forKey: "quantity")
        self.productArray.append(self.para as! [String : String])
        print("Product Dictionary did select : (self.productArray)  || (self.para)")
        
        UserDefaults.standard.set(self.selectedProductID, forKey:"kProdutID")
       
            self.selectedProductsCount += 1
            self.moveBasketButton.setTitle("Move (self.selectedProductsCount) items to basket", for: .normal)
      
    
    // navigate to store details
   // self.navigatTo(identifier: "StoreDetailsVC", animated: true)
    }
    else {
        print("nothing implemented")
    }
}

But I didn't implemented didDeselect method to remove those keys and values from the main dict rather than the uistate only so please tell me how to remove the selected values from this array.

Please someone guide me properly to get this sorted. I am lacking on experience but need to sort this very soon.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...