【发布时间】:2020-04-25 23:54:31
【问题描述】:
我正在尝试删除 ForEach 中的行。删除最后一行总是会引发索引超出范围异常。删除任何其他行不会。
ForEach(Array(player.scores.enumerated()), id: \.element) { index, score in
HStack {
if self.isEditSelected {
Button(action: {
self.player.scores.remove(at: index)
}, label: {
Image("delete")
})
}
TextField("\(score)", value: self.$player.scores[index], formatter: NumberFormatter())
}
}
我尝试过使用ForEach(player.indices...) 和ForEach(player.scores...),但遇到了同样的问题。
在我看来,崩溃发生在这里self.$player.scores[index],因为将索引硬编码为除最后一行工作之外的任何值。
有谁知道如何解决这个问题?或者如果有更好的方法。
【问题讨论】:
-
这能回答你的问题吗? Deletable Table with TextField on SwiftUI