列表中的奇怪按钮行为(SwiftUI)

Strange buttons behaviour in a list (SwiftUI)

在下面的 SwiftUI 代码中,我注意到了一些意外行为。

我想知道这是否是一个错误,这是正常现象还是我只是遗漏了一些明显的东西。

List {
    ForEach(self.myList, id: \.self.name) {
        item in
        HStack {
            Spacer()
            Button(action: {
                print("Button One tapped!")
                ....
            }) {
                item.name.map(Text.init)
                    .font(.largeTitle)
                    .foregroundColor(.secondary)
            }
            Spacer()
            Button(action: {
                print("Button Two tapped!")
                ....
            }) {
                Image(systemName: "pencil.circle")
                    .font(.title)
                    .foregroundColor(.secondary)
                    .padding(.leading, 17)
            }
        }
    }
    .onDelete(perform: deleteFunc)
}

下面是连续点击两个按钮之一时发生的情况。 我可以看到这两条消息:

Button One tapped!
Button Two tapped!

我预计只会看到一条消息,具体取决于点击的按钮。

如果消息的顺序根据点击的按钮而变化;我可以使用一两个布尔值来强制执行我想要的最终结果。但是两条消息总是以相同的顺序出现。

有没有人有过同样的经历?或者有没有人看到任何错误?

使用PlainButtonStyle(或任何自定义的),因为列表会自动检测默认按钮样式以突出显示整行。

这是一个简化的(来自您的代码)演示:

struct DemoListWithButtons: View {
    var body: some View {
        List {
            ForEach(0..<5, id: \.self) {
                item in
                HStack {
                    Spacer()
                    Button(action: {
                        print("Button One tapped!")
                    }) {
                        Text("First")
                    }.buttonStyle(PlainButtonStyle()) // << here !!
                    Spacer()
                    Button(action: {
                        print("Button Two tapped!")
                    }) {
                        Text("Second")
                    }.buttonStyle(PlainButtonStyle()) // << here !!
                }
            }
        }
    }
}