首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Did DeSelect上删除字典数组中的值?

如何在Did DeSelect上删除字典数组中的值?
EN

Stack Overflow用户
提问于 2017-05-18 18:42:06
回答 3查看 452关注 0票数 0

我正在用OrderDetailsArray填充我的tableView。然后在DidSelect上,我给reorderArray增加了价值。如何在点击DidDeSelect时从数组中删除值

我的tableView委托的功能如下:

代码语言:javascript
复制
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()

EN

回答 3

Stack Overflow用户

发布于 2017-05-18 19:11:38

除非有充分的理由,否则您不应该在Swift中使用诸如NSMutableArrayNSDictionary之类的基础类,但简单的答案是您可以使用removeNSMutableArray中删除对象的实例,并且您可以很容易地从索引路径中找到相关对象:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-05-18 20:09:46

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

代码语言:javascript
复制
func tableView(_ tableView: UITableView, didDeSelectRowAt indexPath: IndexPath)
 {
       let dicDetails = arrUserDetails[indexPath.row] as! NSMutableDictionary
       dicDetails.removeObject(forKey: "your_Key")

 }
票数 0
EN

Stack Overflow用户

发布于 2017-05-18 22:26:24

重试一次

代码语言:javascript
复制
 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)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44045550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档