是否可以在 SwiftUI 的 List/ForEach 视图中使用 Button(action: -)

Is it possible to have a Button(action: -) in a List/ForEach view in SwiftUI

我想将 Button(action: - 视图添加到列表视图中,其中包含 ForEach,其中包含 NavigationLink。因此,用户可以查看具有 Button(action: - 的列表的详细信息屏幕,但是如果用户不需要查看详细信息,则可以使用列表中的 Button select。

List {
  ForEach(DATA, id: \.self { data in
    NavigationLink(destination: DetailView(data: data)) {
      HStack {
        Text(data.name)
        Button(action: {
           // add to an array
        }) {
             Text(data.price)
        }
      }
    }
  }
}

但是当我点击按钮时总是会转到 DetailView

不确定是否可行。任何建议

这是可能的方法

List {
  ForEach(DATA, id: \.self { data in
    NavigationLink(destination: DetailView(data: data)) {
      HStack {
        Text(data.name)
        Button(action: {
           // add to an array
        }) {
             Text(data.price) // some padding might be useful
                .foregroundColor(Color.blue) // << can be any 
        }.buttonStyle(PlainButtonStyle())    // << here !!
      }
    }
  }
}