SwiftUI列出如何识别在macOS上选择了什么项目 [英] SwiftUI List how to identify what item is selected on macOS
问题描述
Here is what I have based on this answer. The code currently allows the user to select a cell but I cannot distinguish which cell is selected or execute any code in response to the selection. In summary, how can I execute code based on the selected cell's name and execute on click. The cell currently highlights in blue where clicked, but I want to identify it and act accordingly based on that selection. Note: I am not looking to select the cell in editing mode. Also, how can I programmatically select a cell without click?
struct OtherView: View {
@State var list: [String]
@State var selectKeeper = Set<String>()
var body: some View {
NavigationView {
List(list, id: \.self, selection: $selectKeeper) { item in
Text(item)
}
}
}
}
这是一个演示选择的 gif
Here is a gif demoing the selection
推荐答案
我找到了一种解决方法,但必须单击文本本身-单击单元格无济于事:
I found a workaround, but the text itself has to be clicked- clicking the cell does nothing:
struct OtherView: View {
@State var list: [String]
@State var selectKeeper = Set<String>()
var body: some View {
NavigationView {
List(list, id: \.self, selection: $selectKeeper) { item in
Text(item)
.onTapGesture {
print(item)
}
}
}
}
}
这篇关于SwiftUI列出如何识别在macOS上选择了什么项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!