Swift 切换列表中的选择-快捷界面
我希望我能把我的问题解释清楚 我想通过切换从列表中选择一些课程,但无论我尝试了什么,都没有成功 我该怎么办 谢谢你抽出时间。 最好的, 穆拉特 当然部分Swift 切换列表中的选择-快捷界面,swift,foreach,swiftui,toggle,Swift,Foreach,Swiftui,Toggle,我希望我能把我的问题解释清楚 我想通过切换从列表中选择一些课程,但无论我尝试了什么,都没有成功 我该怎么办 谢谢你抽出时间。 最好的, 穆拉特 当然部分 import Foundation import SwiftUI struct Course: Codable, Identifiable { var id: Int var title: String var subjectCount: String var courseName: [Content
import Foundation
import SwiftUI
struct Course: Codable, Identifiable {
var id: Int
var title: String
var subjectCount: String
var courseName: [Content]
var isToggled = false
private var imageName: String
var image: Image {
Image(imageName)
}
enum LessonSegment: String, CaseIterable, Identifiable {
case overview
case resources
var id: String { self.rawValue }
}
enum CodingKeys: String, CodingKey {
case id
case title
case subjectCount
case imageName
case courseName
}
}
struct Content: Codable {
var id: Int
var name: String
var content: String
var assessment: String
var notify: String
}
你的
@State-private-var开关:Bool=false
没有意义。你有很多课程,不是一门。每门课程都应该有自己的开关,这是您开始使用的:
struct Course: Codable, Identifiable {
var isToggled = false /// here!
...
}
要使用此选项,您可以在ForEach中引用每个课程
的i切换
,如下所示:
ForEach(courses) { course in
Section(header: Text(course.title).font(.system(size: 15, weight: .medium, design: .rounded)).foregroundColor(.blue)) {
ForEach(course.courseName, id: \.name) { item in
/// here!
Toggle(isOn: course.isToggled, label: {
Text(item.name)
})
}
}
}
然而,这是行不通的course.isToggled
是一个Bool
,而不是切换所期望的绑定
在哪里可以获得绑定
?从@State var courses:[课程]
,当然!对不起,这是双关语
绑定
部分来自@State
声明。
标有@State
的属性,如@State var课程:[Course]
,包括具有绑定类型的属性
您可以通过向属性添加$
来访问projectedValue
。因此,如果您编写$courses
,那么将具有类型绑定
但是,您的切换需要的是绑定
,而不是绑定
这就是Bool
部分的作用。
您需要将绑定的值[Course]
替换为Bool
。嗯,我们以前有过一个Bool
,对吧
struct Course: Codable, Identifiable {
var isToggled = false /// this is a Bool!
每门课程都有一个isToggled
,这是一个Bool
。从前面的回答中,我们在ForEach
中得到了这一点:
ForEach(courses) { course in
...
/// getting the Bool, which unfortunately doesn't work (yet)
Toggle(isOn: course.isToggled, label: {
。。。我们需要以某种方式将绑定
与Bool
结合起来。这意味着我们必须
- 参考
$courses
(获取绑定
)
- 获取每个课程的“
i切换”
而且。。。塔达
$courses[index].isToggled /// has type Binding<Bool>
然后,只需将旧代码的ForEach
中出现的课程
替换为课程[索引]
。以下是完整的工作示例:
ForEach(courses.indices) { index in
Section(header: Text(courses[index].title).font(.system(size: 15, weight: .medium, design: .rounded)).foregroundColor(.blue)) {
ForEach(courses[index].courseName, id: \.name) { item in
/// $courses[index].isToggled is a Binding<Bool>
Toggle(isOn: $courses[index].isToggled, label: {
Text(item.name)
})
}
}
}
当然,(Int,Course)
实际上是(Range.Element,Course)
,但这几乎是一样的
最终结果:
在内容
中编辑i切换
,而不是课程
:
ForEach(Array(zip(courses.indices, courses)), id: \.1.id) { (index, course) in
Section(header: Text(course.title).font(.system(size: 15, weight: .medium, design: .rounded)).foregroundColor(.blue)) {
ForEach(Array(zip(course.courseName.indices, course.courseName)), id: \.1.id) { (itemIndex, item) in
/// here!
Toggle(isOn: $courses[index].courseName[itemIndex].isToggled, label: {
Text(item.name)
})
}
}
}
你能包括课程的代码吗?是的。我编辑了原始消息。所以您想切换是否选中了每个内容?内容
上是否有某个属性应响应切换?看起来您在课程
上有一个i切换
,但您的切换
控件位于课程名称
的ForEach
下,因此有点不清楚。第二,您是否坚持使用Int
id,或者是否可以使用更独特的UUIDs?每个courseName都有一个切换,这样我就可以了解选择了哪个选项。选择后,将在内容视图中保存和过滤。我尝试使用UUID和Int ID,但它们不起作用。感谢您编辑我的问题和您的答案。它起作用了。差不多两天了,我一直在努力。我今天学到了一些东西@穆拉迪我补充了一些额外的澄清我的回答:)再次感谢你。我想再问一个问题。每个课程都有两个或多个CourseName,toggle再次作为一个块工作。很抱歉打扰您。@muradi当然,是什么?我正在上传屏幕共享。
ForEach(courses.indices) { index in
Section(header: Text(courses[index].title).font(.system(size: 15, weight: .medium, design: .rounded)).foregroundColor(.blue)) {
ForEach(courses[index].courseName, id: \.name) { item in
/// $courses[index].isToggled is a Binding<Bool>
Toggle(isOn: $courses[index].isToggled, label: {
Text(item.name)
})
}
}
}
ForEach(Array(zip(courses.indices, courses)), id: \.1.id) { (index, course) in
Section(header: Text(course.title).font(.system(size: 15, weight: .medium, design: .rounded)).foregroundColor(.blue)) {
ForEach(course.courseName, id: \.name) { item in
Toggle(isOn: $courses[index].isToggled, label: {
Text(item.name)
})
}
}
}
ForEach(Array(zip(courses.indices, courses)), id: \.1.id) { (index, course) in
Section(header: Text(course.title).font(.system(size: 15, weight: .medium, design: .rounded)).foregroundColor(.blue)) {
ForEach(Array(zip(course.courseName.indices, course.courseName)), id: \.1.id) { (itemIndex, item) in
/// here!
Toggle(isOn: $courses[index].courseName[itemIndex].isToggled, label: {
Text(item.name)
})
}
}
}