2017-05-18 54 views
0

我正在使用OrderDetailsArray填充我的tableView。然後在DidSelect我爲reorderArray增值。 如何從輕敲DidDeSelect的陣列中刪除值?如何刪除Did DeSelect字典數組中的值?

我的tableView委託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是NSMutableArray()

+0

可以嘗試配股代碼'reorderArray.removeObjects(在:索引) cell.contentView.backgroundColor = UIColor.clear 打印(reorderArray)'內關閉 – Priyal

+0

它不能完成,因爲閉包返回值,所以如果我這樣做,那麼我得到一個錯誤:在它自己的初始值內使用的變量 –

回答

1

你不應該使用基礎類斯威夫特如NSMutableArrayNSDictionary,除非你有一個很好的理由,但簡單的答案就是你可以使用removeNSMutableArray中刪除對象的實例,並且您可以從索引路徑輕鬆找到相關對象:

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

在Swift 3.0中,你可以試試這個,當使用Array of Dictionary時,你可以這樣做。

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

} 
0

一次嘗試

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) 
} 
相關問題