【发布时间】:2020-09-02 09:46:43
【问题描述】:
我想以编程方式设置列表中的选定行。
在下面的示例中,通过 2 个按钮。
struct ContentView: View {
@State private var selection = 2
var body: some View {
VStack {
List() {
//List(selection: $selection) {. // does not compile
Text("Line 0").tag(1)
Text("Line 1").tag(1)
Text("Line 2").tag(2)
Text("Line 3").tag(3)
Text("Line 4").tag(4)
Text("Line 5").tag(5)
}
.listStyle(SidebarListStyle())
Text("Selected Item :\(self.selection)")
HStack {
Button(action: {if (self.selection < 5 ) { self.selection += 1 }} ) {Text("⬇︎")}
Button(action: {if (self.selection > 0 ) { self.selection -= 1 }} ) {Text("⬆︎")}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
尝试像这样使列表可选择:
List(selection: $selection)
无法编译。
编译器抱怨:Unable to infer complex closure return type; add explicit type to disambiguate
【问题讨论】: