Ada*_*dam 4 iphone ios swift swiftui
我遇到了一些列表问题,该列表不会改变行的高度。我把它分成一个简单的例子。
这是整个演示代码:
struct ContentView: View {
let items = ["item 1", "item 2", "item 3"]
@State var error: [String: String] = []
var body: some View {
List {
ForEach(self.items, id: \.self) { item in
VStack {
Text(item)
.padding()
if let e = error[item] {
Text(e)
}
}
.swipeActions(edge: .leading, allowsFullSwipe: true) {
Button(action: {
self.error[item] = "ERROR!"
}) {
Image(systemName: "square.and.arrow.up")
}.background(Color.blue)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
一旦用户滑动单元格,我想在常规项目下方显示一条错误消息。不幸的是现在发生的是这样的:
我希望它看起来像这样(为此我在代码中的“错误”字典中添加了一个元素):
在我看来,这是很多人都会遇到的问题,但我无法找到有关此问题的太多信息。我尝试过一些东西(例如“.fixedSize”修饰符),但我尝试过的任何东西都不起作用。
列表缓存创建的行,因此我们需要让它知道特定的行应该从头开始重建。可能的解决方案是使用.id
与可选错误文本相同的条件。
使用 Xcode 13.2 / iOS 15.2 进行测试
这是修改后的部分代码(为演示添加了一些额外的代码):
.swipeActions(edge: .leading, allowsFullSwipe: true) {
Button(action: {
if nil == self.error[item] {
self.error[item] = "ERROR!"
} else {
self.error[item] = nil
}
}) {
Image(systemName: "square.and.arrow.up")
}.background(Color.blue)
}
.id(item + (self.error[item] ?? "")) // << here !!
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2312 次 |
最近记录: |