快速更新表格视图单元格

时间:2019-07-09 05:20:41

标签: swift uitableview multidimensional-array row indexpath

背景

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

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

 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 loop中做这样的事情:

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

1 个答案:

答案 0 :(得分:2)

也许您可以尝试一下。祝您好运,如果无法解决,请发表评论。我是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
  }
}