SwiftUI-列表行中的多个按钮

假设我List在一行中有一个和两个按钮,如何在不突出显示整个行的情况下区分哪个按钮被轻拍了?

对于此示例代码,当点击该行中的任何一个按钮时,将同时调用两个按钮的动作回调。

// a simple list with just one row

List {

// both buttons in a HStack so that they appear in a single row

HStack {

Button(action: {

print("button 1 tapped")

}) {

Text("One")

}

Button(action: {

print("button 2 tapped")

}) {

Text("Two")

}

}

}

// when tapping just once on either button:

// "button 1 tapped"

// "button 2 tapped"

回答:

您需要使用

List([1, 2, 3], id: \.self) { row in

HStack {

Button(action: {

print("Buton at \(row) with name A")

}) {

Text("Row: \(row)" + " Name: A")

}.buttonStyle(BorderlessButtonStyle())

Button(action: {

print("Buton at \(row) with name B")

}) {

Text("Row: \(row)" + " Name: B")

}.buttonStyle(BorderlessButtonStyle())

}

}

以上是 SwiftUI-列表行中的多个按钮 的全部内容, 来源链接: utcz.com/qa/420206.html

回到顶部