List ctKeeper=Set() var body:一些观点{ 导航视图{ 列表(解调数据,id:\.self,选择:$selectKeeper){name in 文本(名称) } //下一行是修改器 .environment(\.editMode、.constant(editMode.active)) .navigationBarTitle(文本(“选择演示\(selectKeeper.count)”) } } }
我不使用编辑模式,而是根据模型更新该行,并根据建议点击该行时在模型中切换布尔值。也许是这样的:List ctKeeper=Set() var body:一些观点{ 导航视图{ 列表(解调数据,id:\.self,选择:$selectKeeper){name in 文本(名称) } //下一行是修改器 .environment(\.editMode、.constant(editMode.active)) .navigationBarTitle(文本(“选择演示\(selectKeeper.count)”) } } },list,selection,swiftui,List,Selection,Swiftui,我不使用编辑模式,而是根据模型更新该行,并根据建议点击该行时在模型中切换布尔值。也许是这样的: struct MultipleSelectionRow<RowContent: SelectableRow>: View { var content: Binding<RowContent> var body: some View { Button(action: { self.content.value.isSelec
struct MultipleSelectionRow<RowContent: SelectableRow>: View {
var content: Binding<RowContent>
var body: some View {
Button(action: {
self.content.value.isSelected.toggle()
}) {
HStack {
Text(content.value.text)
Spacer()
Image(systemName: content.value.isSelected ? "checkmark.circle.fill" : "circle")
}
}
}
}
然后你可以做如下事情:
struct Person: Hashable, Identifiable, SelectableRow {
let id = UUID().uuidString
let text: String
var isSelected: Bool = false
}
struct ContentView : View {
@State var people: [Person] = [
Person(text: "Mo"),
Person(text: "Larry"),
Person(text: "Curly")
]
var body: some View {
List {
ForEach($people.identified(by: \.id)) { person in
MultipleSelectionRow(content: person)
}
}
}
}
屈服:
我的回答有用吗?还是我误解了你的问题?我不知道OP,但你帮了我!我没有意识到Set实现了SelectionManager协议!谢谢抱歉@piebie,你的回答非常有用。非常感谢。太神了是否可以像picker那样将其添加到表单中?这显然是解决此问题的最佳方法。但是,关于如何避免视图显示和“切换”到编辑模式之间的延迟的任何提示?SelectKeeper不使用,也不需要
protocol SelectableRow {
var text: String { get }
var isSelected: Bool { get set }
}
struct Person: Hashable, Identifiable, SelectableRow {
let id = UUID().uuidString
let text: String
var isSelected: Bool = false
}
struct ContentView : View {
@State var people: [Person] = [
Person(text: "Mo"),
Person(text: "Larry"),
Person(text: "Curly")
]
var body: some View {
List {
ForEach($people.identified(by: \.id)) { person in
MultipleSelectionRow(content: person)
}
}
}
}