Rajat Attri
Rajat Attri

Reputation: 73

How to remove value inside Array of Dictionary on Did DeSelect?

I am populating my tableView with OrderDetailsArray. Then on DidSelect I am adding value to reorderArray. How to remove value from array on tapping DidDeSelect?

My tableView delegate funcs:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let object = orderDetailsArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary

    let storeID = UserDefaults.standard.value(forKey: DefaultsKey.storeID.rawValue) as! Int
    let barcode = object["barcode"] as! String
    let mrp = object["mrp"] as! String
    let productDiscount = object["product_discount"] as! String

    reorderDictionary.setObject((storeID), forKey: "store_id" as NSCopying)
    reorderDictionary.setObject((barcode), forKey: "barcode" as NSCopying)
    reorderDictionary.setObject((mrp), forKey: "mrp" as NSCopying)
    reorderDictionary.setObject((productDiscount), forKey: "product_discount" as NSCopying)

    reorderArray.add(reorderDictionary)
    let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
    cell.contentView.backgroundColor = UIColor.red
    print(reorderArray)
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
    let object = reorderArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary
    reorderArray.remove(object)
    cell.contentView.backgroundColor = UIColor.clear
    print(reorderArray)
}

reorderArray is NSMutableArray()

Upvotes: 0

Views: 506

Answers (3)

Ramesh_iOS
Ramesh_iOS

Reputation: 11

Try Once

 1.Create One more array globally and store indexpaths insted of reorderArray

OR

2.Apply this lines of code in TableViewCellForRowAtIndexPath:

if reorderArray.contains(reorderDictionary){
   cell.contentView.backgroundColor = UIColor.red
}else{
   cell.contentView.backgroundColor = UIColor.clear
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     let object = orderDetailsArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary

     let storeID = UserDefaults.standard.value(forKey: DefaultsKey.storeID.rawValue) as! Int
     let barcode = object["barcode"] as! String
     let mrp = object["mrp"] as! String
     let productDiscount = object["product_discount"] as! String

     reorderDictionary.setObject((storeID), forKey: "store_id" as NSCopying)
     reorderDictionary.setObject((barcode), forKey: "barcode" as NSCopying)
     reorderDictionary.setObject((mrp), forKey: "mrp" as NSCopying)
     reorderDictionary.setObject((productDiscount), forKey: "product_discount" as NSCopying)
     let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
     if reorderArray.contains(reorderDictionary){
        reorderArray.remove(reorderDictionary)
        cell.contentView.backgroundColor = UIColor.clear
     }else{
      reorderArray.add(reorderDictionary)
      cell.contentView.backgroundColor = UIColor.red
     }

    print(reorderArray)
}

Upvotes: 0

Berlin
Berlin

Reputation: 2227

In Swift 3.0, you can try this, when used Array of Dictionary, you can do this.

func tableView(_ tableView: UITableView, didDeSelectRowAt indexPath: IndexPath)
 {
       let dicDetails = arrUserDetails[indexPath.row] as! NSMutableDictionary
       dicDetails.removeObject(forKey: "your_Key")

 }

Upvotes: 0

Paulw11
Paulw11

Reputation: 114866

You shouldn't use Foundation classes such as NSMutableArray or NSDictionary in Swift unless you have a good reason, but the simple answer is you can use remove to remove instances of an object from an NSMutableArray and you can easily find the relevant object from the index path:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
    let object = orderDetailsArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary
    reorderArray.add(object)
    cell.contentView.backgroundColor = UIColor.red
    print(reorderArray)
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let object = orderDetailsArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary
    reorderArray.remove(object)
    let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
    cell.contentView.backgroundColor = UIColor.clear
    print(reorderArray)
}

Upvotes: 1

Related Questions