ios - 如何在 Did DeSelect 中删除字典数组中的值?

标签 ios swift uitableview uikit

我正在用 OrderDetailsArray 填充我的 tableView。然后在 DidSelect 上,我将值添加到 reorderArray。 如何在点击 DidDeSelect 时从数组中删除值?

我的 tableView 委托(delegate)函数:

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

最佳答案

你不应该在 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)
}

关于ios - 如何在 Did DeSelect 中删除字典数组中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44045550/

相关文章:

swift - 如何快速将自身转换为UnsafeMutablePointer <Void>类型

ios - UITableView 在像 Facebook 应用程序一样滚动到底部时加载更多

ios - 使用 Swift 提取最重要的半字节

ios - 在 cocos2d-iphone 中制作启动画面动画?

ios - 从一个 View Controller 移动到另一个 View Controller 时如何保存表格 View 数据?

ios - 指定 UITableView 中的项目数 - iOS

ios - 可重复使用的电池旧图像显示

ios - AVAssetExportSession 无音频(iPhone),适用于 iPad

ios - 调用 removeFromSuperView() 后,SubView(nib) 不会删除

ios - UITextView 类,从 Objective-C 到 Swift 的 intrinsicContentSize 问题