如何删除DeSelect中字典数组中的值?

时间:2017-05-18 10:42:06

标签: ios swift uitableview uikit

我正在使用tableView填充OrderDetailsArray。然后在DidSelect我正在为reorderArray增加价值。 如何在点击DidDeSelect上删除数组中的值?

我的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()

3 个答案:

答案 0 :(得分:1)

除非你有充分的理由,否则你不应该在Swift中使用NSMutableArrayNSDictionary等基础类,但简单的答案是你可以使用remove删除一个实例。来自NSMutableArray的对象,您可以轻松地从索引路径中找到相关对象:

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

答案 1 :(得分:0)

在Swift 3.0中,你可以试试这个,当使用Dictionary of Dictionary时,你可以这样做。

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

 }

答案 2 :(得分: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)
}