这可以在 SwiftUI 中轻松完成。您可以使用 VStack(或 LazyVStack 或 LazyVGrid)来设置列表视图(比 TableViews 容易得多),然后制作一个看起来像其中一个视图的自定义、可重用视图并将其设置为按钮中的标签。这里有一些代码(简化)可以帮助您入门:
import SwiftUI
struct CustomView: View {
var body: some View {
VStack {
Button(action: {
print("First button pressed")
}, label: {
CustomRowView(buttonTitle: "First Button")
})
Button(action: {
print("Second button pressed")
}, label: {
CustomRowView(buttonTitle: "Second Button")
})
Button(action: {
print("Third button pressed")
}, label: {
CustomRowView(buttonTitle: "Third Button")
})
Spacer()
}
.padding()
}
}
struct CustomRowView: View {
@State var buttonTitle: String
var body: some View {
Text(buttonTitle)
.frame(maxWidth: .infinity)
.foregroundColor(.white)
.padding(.vertical, 40)
.background(Color.blue)
.cornerRadius(12)
}
}
struct CustomViewsz_Previews: PreviewProvider {
static var previews: some View {
CustomView()
}
}