正在更新 table 个视图单元格 swift
Updating table view cells swift
背景
我有一个简单的账单拆分应用程序,允许用户将餐点分配给多个人或用户。当餐点分给多人时,价格相应平分。膳食(其中包含项目名称和价格是行,用户是部分。
当我删除一行时,我想删除该行,并更新或更改某些其他值(我基本上想在用户删除某项时将价格重新分配给少一个人)。我的数据模型是 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({[=13=].itemModels.contains(where: {[=13=].itemName == item})}).filter({[=13=].name != user})
for i in 0..<duplicate.count {
???
}
tableView.reloadData()
actionPerformed(true)
}
return UISwipeActionsConfiguration(actions: [delete])
}
变量 var duplicate
returns 在 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.section
和 indexPath.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({[=15=].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
}
}
背景
我有一个简单的账单拆分应用程序,允许用户将餐点分配给多个人或用户。当餐点分给多人时,价格相应平分。膳食(其中包含项目名称和价格是行,用户是部分。
当我删除一行时,我想删除该行,并更新或更改某些其他值(我基本上想在用户删除某项时将价格重新分配给少一个人)。我的数据模型是 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({[=13=].itemModels.contains(where: {[=13=].itemName == item})}).filter({[=13=].name != user})
for i in 0..<duplicate.count {
???
}
tableView.reloadData()
actionPerformed(true)
}
return UISwipeActionsConfiguration(actions: [delete])
}
变量 var duplicate
returns 在 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.section
和 indexPath.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({[=15=].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
}
}