如何在tableView DidSelect上更新Dictionary数组中特定键的值?

时间:2017-05-23 08:35:48

标签: swift xcode uitableview nsmutablearray

下面提到的是我的数组,我已将这些词典添加到NSMutableArray中。当我点击tableView中的单元格时,我想将order_product_type的值从Delivery更改为Pickup。

({
    name = "Colgate Cibaca";
    "order_product_type" = DELIVERY;
    productTotal = "30.00";
    "product_id" = 32;
    quantity = 1;
},{
    name = "Lays Classic Salted";
    "order_product_type" = DELIVERY;
    productTotal = "20.00";
    "product_id" = 33;
    quantity = 1;
})

请帮我处理代码。

2 个答案:

答案 0 :(得分:3)

你可以尝试这个 var arr = [dict1,dict2]。在didSelect中

var dictt = arr[indexpath.item] 
dictt["order_product_type"] = "picked";
arr.remove(at: indexpath.item);
arr.insert(dictt, at: indexpath.item);

答案 1 :(得分:0)

它有帮助

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let dataObject = productArray[indexPath.row] as? NSDictionary

    DataDict = [
        "name"                     :   (dataObject?["name"] as? String)!,
        "productTotal"             :   (dataObject?["productTotal"] as? String)!,
        "quantity"                 :   (dataObject?["quantity"] as? Int)!,
        "product_id"               :   (dataObject?["product_id"] as? Int)!,
        "order_product_type"       :   "PICKUP"
    ]
    productArray.replaceObject(at: indexPath.row, with: DataDict)
    print(productArray)
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let dataObject = productArray[indexPath.row] as? NSDictionary

        DataDict = [
            "name"                     :   (dataObject?["name"] as? String)!,
            "productTotal"             :   (dataObject?["productTotal"] as? String)!,
            "quantity"                 :   (dataObject?["quantity"] as? Int)!,
            "product_id"               :   (dataObject?["product_id"] as? Int)!,
            "order_product_type"       :   "DELIVERY"
        ]
        productArray.replaceObject(at: indexPath.row, with: DataDict)
        print(productArray)
    }