简体   繁体   English

快速更新表格视图单元格

[英]Updating table view cells swift

BACKGROUND 背景

I have a simple bill splitting app that allows the users to assign a meal item to multiple people or users. 我有一个简单的账单拆分应用程序,它允许用户将餐点分配给多个人或用户。 When the meal is assigned to multiple people, the price is divided accordingly. 当膳食分配给多个人时,价格将相应地分配。 The meal (which contains an item name and a price are the rows and the users are the sections. 餐(其中包含商品名称和价格,行是用户,区域是用户。

When I delete a row, I want to delete the row, and update or alter certain other values (I basically want to reassign the price to one less person when an item is deleted from a user). 当我删除一行时,我想删除该行,并更新或更改某些其他值(从用户删除某项商品时,我基本上是想将价格重新分配给少一位)。 My data model is a multidimensional array . 我的数据模型是multidimensional array Here it is: 这里是:

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

For example, in trailingSwipeActionsConfigurationForRowAt 例如,在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])
}

The variable var duplicate returns an array of the other users who have the same item at the indexPath.row , but not the user( indexPath.section ) who has the item. 变量var duplicate返回谁拥有在同一项目的其他用户的数组indexPath.row ,但不是用户( indexPath.section )谁拥有的项目。 I know it sounds really confusing, but I can provide more code or print statements if needed. 我知道这听起来确实令人困惑,但是如果需要,我可以提供更多代码或print语句。

Also in the for loop , I want to do something like this: 同样在for loop ,我想做这样的事情:

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

But I can't access the price. 但是我无法获得价格。 I believe need an indexPath.section and indexPath.row . 我相信需要一个indexPath.sectionindexPath.row

If you made it this far, thank you for taking the time. 如果您走了这么远,谢谢您抽出宝贵的时间。 I feel like I need a nested loop, but I'm not sure how exactly to implement that. 我觉得我需要一个嵌套循环,但是我不确定如何实现它。 If there are any other easier ways to achieve this I'm open to any suggestions. 如果还有其他更简单的方法可以实现这一目标,我欢迎您提出任何建议。

Thank you so much! 非常感谢!

EDIT: The marked answer worked! 编辑:标记的答案有效! In case anyone else was having a similar issue this is what my final code looks like in the trailingSwipeActionsConfigurationForRowAt : 万一其他人遇到类似的问题,这就是我的最终代码在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])
}

Perhaps you can try this. 也许您可以尝试一下。 Best of luck, please comment if it doesn't work. 祝您好运,如果无法解决,请发表评论。 I am new to Swift :) 我是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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM