我正在尝试为用户使用列表选择来从列表中选择零天或多天。我曾尝试使用此问题中列出的解决方案,但无济于事。
我的代码如下所示,并且我已验证编辑模式处于活动状态。我应该期待上面链接的答案中的行为。
代码:
struct EditView: View {
@State var selectKeeper = Set<String>()
var weekdays: [String] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
@Environment(\.editMode) var mode
var body: some View {
Form {
Section(header: Text("Overtime Days")) {
List(self.weekdays, id: \.self, selection: $selectKeeper) { day in
Text(day)
}
}
} .navigationBarTitle(Text("My Title"))
.padding(.top)
.onAppear(perform: {
print(self.mode?.value as Any)
})
}
}
EditButton 包含在父视图中,并在以下代码中启用。
struct JobDetailHost: View {
@Environment(\.editMode) var mode
@Binding var jobDetails: JobDetails
var body: some View {
VStack {
if self.mode?.value == .inactive {
JobDetailView(jobDetails: jobDetails)
} else {
EditView(jobDetails: $jobDetails)
.onDisappear(perform: {
//DO STUFF...
})
}
}
.navigationBarItems(trailing: EditButton())
.onAppear(perform: bindDraft)
}
}
该表应显示其他答案中的选择行为,但目前它在编辑模式下仅显示为普通列表。