swift - 快速更新表格 View 单元格

标签 swift uitableview multidimensional-array row indexpath

背景

我有一个简单的账单拆分应用程序,允许用户将餐点分配给多个人或用户。当餐点分给多人时,价格相应平分。膳食(包含项目名称和价格是行,用户是部分。

当我删除一行时,我想删除该行,并更新或更改某些其他值(我基本上想在用户删除某项时将价格重新分配给少一个人)。我的数据模型是一个多维 数组。在这里:

 struct UserModel {
    var name: String
    var itemModels = [ItemModel]()

    init(name: String, itemModels: [ItemModel]? = nil) {
        self.name = name
        if let itemModels = itemModels {
            self.itemModels = itemModels
        }
    }
}

    struct ItemModel {
    var itemName: String
    var price: Double

    init(itemName: String, price: Double) {
        self.itemName = itemName
        self.price = price
    }
}
    class Data {
    static var userModels = [UserModel]()
    static var itemModels = [ItemModel]()
}

例如,在 trailingSwipeActionsConfigurationForRowAt

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let delete = UIContextualAction(style: .destructive, title: "Delete") { (contextualAction, view, actionPerformed: @escaping (Bool) -> ()) in

        let user = Data.userModels[indexPath.section].name
        let item = Data.userModels[indexPath.section].itemModels[indexPath.row].itemName
        let price = Data.userModels[indexPath.section].itemModels[indexPath.row].price


        ItemModelFunctions.removeFromUser(from: indexPath.section, remove: indexPath.row)

        var duplicate = Data.userModels.filter({$0.itemModels.contains(where: {$0.itemName == item})}).filter({$0.name != user}) 

        for i in 0..<duplicate.count {
            ???
        }
        tableView.reloadData()
        actionPerformed(true)
    }
    return UISwipeActionsConfiguration(actions: [delete])
}

变量 var duplicate 返回在 indexPath.row 有相同项目的其他用户的数组,但不是用户(indexPath.section ) 谁拥有该元素。我知道这听起来很困惑,但如果需要,我可以提供更多代码或 print 语句。

同样在for循环中,我想做这样的事情:

for i in 0..<duplicate.count {
    let oldPrice = duplicate[i].price
    let newPrice = oldPrice * duplicate.count
    duplicate[i].price = newPrice
}

但我无法访问价格。我认为需要 indexPath.sectionindexPath.row

如果您做到了这一点,感谢您抽出宝贵时间。我觉得我需要一个嵌套循环,但我不确定具体如何实现它。如果有任何其他更简单的方法来实现这一点,我愿意接受任何建议。

非常感谢!

编辑: 标记的答案有效!如果其他人遇到类似问题,这就是我在 trailingSwipeActionsConfigurationForRowAt 中的最终代码:

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let delete = UIContextualAction(style: .destructive, title: "Delete") { (contextualAction, view, actionPerformed: @escaping (Bool) -> ()) in

        let item = Data.userModels[indexPath.section].itemModels[indexPath.row]
        ItemModelFunctions.removeFromUser(from: indexPath.section, remove: indexPath.row)

        let duplicate = Data.userModels.filter({$0.itemModels.contains(item)})

        for i in 0..<Data.userModels.count {
            if let idx = Data.userModels[i].itemModels.firstIndex(of: item) {
                let oldPrice = Data.userModels[i].itemModels[idx].price

                let newPrice = oldPrice * Double(duplicate.count+1)

                Data.userModels[i].itemModels[idx].price = newPrice / Double(duplicate.count)
            }
        }
        tableView.reloadData()
        actionPerformed(true)
    }
    return UISwipeActionsConfiguration(actions: [delete])
}

最佳答案

也许你可以试试这个。祝你好运,如果它不起作用,请发表评论。我是 Swift 的新手 :)

let actualItem = Data.userModels[indexPath.section].itemModels[indexPath.row]

for i in 0..<duplicate.count {
  if let idx = duplicate[i].itemModels.firstIndex(of: actualItem) {
     let oldPrice = duplicate[i].itemModels[idx].price
     duplicate[i].itemModels[idx].price = oldPrice * duplicate.count
  }
}

关于swift - 快速更新表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56945794/

相关文章:

ios - 如何覆盖 reloadRowsAtIndexPaths?

ios - 不支持推送导航 Controller - 自 Xcode 6.2 更新以来

ios - 如何使表格 View 对齐到单元格中心? (附gif动画图)

ios - 是否可以从 didTapOverlay 方法更改 GMSPolygon fillColor?

iphone - IOS中获取UITableView的行号

c++ - 数组奇怪的参数调理

c - Scanf ("%[^\n]") 不适用于循环内的数组字符串

ios - Swift 中 "Accessing Subscripts of Optional Type"的说明

ios - “MBProgressHUD 需要在主线程上访问。” - swift 3

ios - Lottie 在 iOS 上与用户交互