2017-05-18 4 views
0

Ich bevölke meine tableView mit OrderDetailsArray. Dann auf DidSelect bin ich Mehrwert zu reorderArray hinzufügen. Wie entferne ich den Wert vom Array beim Tippen auf DidDeSelect?So entfernen Sie den Wert im Feld Array of Dictionary on Wurde DeSelect ausgewählt?

Mein Tableview Delegat 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 ist NSMutableArray()

+0

können Sie versuchen, den Code 'reorderArray.removeObjects platzieren (bei: Indizes) cell.contentView.backgroundColor = UIColor.clear print (reorderArray)' innerhalb Schließung – Priyal

+0

Es kann nicht getan werden, weil Schließung Wert zurückgibt, also wenn ich so tue, erhalte ich einen Fehler: Variable, die innerhalb seines eigenen Anfangswerts benutzt wird –

Antwort

1

Sie sollten verwenden Foundation Klassen wie NSMutableArray oder NSDictionary in Swift nicht, wenn Sie einen guten Grund haben, aber die einfache Antwort ist, dass Sie kann remove verwenden, um Instanzen eines Objekts aus einem NSMutableArray zu entfernen, und Sie können das relevante Objekt leicht aus dem Indexpfad finden:

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) 
} 
0

In Swift 3.0 können Sie dies versuchen, wenn Sie Array of Dictionary verwenden, können Sie dies tun.

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

} 
0

noch einmal versuchen

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) 
} 
Verwandte Themen