Swift 使用数据定义阵列中对象的数量

Swift 使用数据定义阵列中对象的数量,swift,swiftui,Swift,Swiftui,我正在SwiftUI中编写一个清单,并希望定义数据中复选框的数量。例如,对于水果,用户应该勾选3个复选框,而水需要更多的复选框,因为你每天需要喝更多的水 我在HStack中使用“for each”来创建一个复选框数组: HStack { ForEach(0 ..< 3) { index in VStack { Button(action: { self.checked[index

我正在SwiftUI中编写一个清单,并希望定义数据中复选框的数量。例如,对于水果,用户应该勾选3个复选框,而水需要更多的复选框,因为你每天需要喝更多的水

我在HStack中使用“for each”来创建一个复选框数组:

 HStack {
           ForEach(0 ..< 3) { index in
           VStack {
               Button(action: {
                  self.checked[index].toggle()
                                    })
有没有办法定义我的数据中复选框的数量


提前谢谢我不知道你是否想要这个

HStack {
       ForEach(0 ..< checklistData[x].steps) { index in
       VStack {
           Button(action: {
              self.checked[index].toggle()
                                })
HStack{
ForEach(0..
好的,这是我更好的答案,谢谢你的代码:

struct ChecklistView: View {
    var checklist: Checklist
    @State var currentProgress: Float = 0.0
    @State var checked = \[false, false, false, false\]


    var body: some View {
        ZStack(alignment: .leading) {

            //            RoundedRectangle(cornerRadius: 20)
            //                .foregroundColor(.red).opacity(0.5)
            //                .frame(width: 200)

            VStack {
                HStack(alignment: .top) {
                    checklist.image
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 40, height: 40)
                        .padding(.trailing)
                    VStack(alignment: .leading) {
                        Text(checklist.title)
                            .font(.system(size: 16, weight: .medium))
                            .padding(.bottom, 4)

                        Text(checklist.instruction.uppercased()).font(.system(size: 12))

                        HStack {
                            ForEach(0 ..< checklist.steps) { index in
                                VStack {
                                    Button(action: {
                                        self.checked\[index\].toggle()
                                    }) {
                                        ZStack {

                                            RoundedRectangle(cornerRadius: 8)
                                                .foregroundColor(self.checked\[index\] ? Color("LightGreen") : .gray )
                                                .frame(width: 40, height: 40)

                                            Image(systemName: self.checked\[index\] ?  "checkmark" : "plus")
                                                .resizable()
                                                .aspectRatio(contentMode: .fit)
                                                .frame(width: 16, height: 16)
                                                .foregroundColor(self.checked\[index\] ? .white : .white)

                                        }
                                    }
                                }
                            }
                        }
                    }
                }.frame(width: 350, alignment: .leading)
            }
        }
        .frame(width: 350)
        .padding(.bottom, 16)
        .cornerRadius(8)
        .padding(.top, 20)
    }
}

// MARK: data checklist
struct Checklist: Identifiable, Hashable {

    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
        hasher.combine(title)
    }

    var id = UUID().uuidString
    var title: String
    var instruction: String
    var image: Image
    var steps: Int
}



struct ContentView: View {

    let checklistData = \[
        Checklist(title: "Fruit", instruction: "1 vakje = 1 stuk of 100g", image: Image("kiwi"), steps: Int(4)),
        Checklist(title: "Fruit", instruction: "1 vakje = 1 stuk of 100g", image: Image("kiwi"), steps: Int(2))

    \]

    var body: some View {
        List(checklistData, id: \.self) { checkList in
            ChecklistView(checklist: checkList)
        }
    }
}
struct ChecklistView:View{
var检查表:检查表
@状态变量currentProgress:Float=0.0
@选中的状态变量=\[false,false,false,false\]
var body:一些观点{
ZStack(对齐:。引导){
//圆角转角(拐角半径:20)
//.foregroundColor(.red).不透明度(0.5)
//.框架(宽度:200)
VStack{
HStack(对齐:。顶部){
检查表.图像
.可调整大小()
.aspectRatio(内容模式:.fit)
.框架(宽度:40,高度:40)
.padding(.training)
VStack(对齐:。前导){
文本(检查表标题)
.font(.system(大小:16,重量:中等))
.padding(.bottom,4)
文本(checklist.instruction.uppercased()).font(.system(大小:12))
HStack{
ForEach(0..<检查表步骤){索引中
VStack{
按钮(操作:{
self.checked\[index\].toggle()
}) {
ZStack{
圆角转角(拐角半径:8)
.foregroundColor(自查\[索引\]?颜色(“浅绿色”):.gray)
.框架(宽度:40,高度:40)
图像(系统名称:self.checked\[索引\]?“复选标记”:“加”)
.可调整大小()
.aspectRatio(内容模式:.fit)
.框架(宽度:16,高度:16)
.foregroundColor(自查\[索引\]?.white:.白色)
}
}
}
}
}
}
}.帧(宽度:350,对齐:。前导)
}
}
.框架(宽度:350)
.padding(.bottom,16)
.转弯半径(8)
.padding(.top,20)
}
}
//标记:数据检查表
结构检查表:可识别、可散列{
func散列(放入散列程序:inout散列程序){
hasher.combine(id)
hasher.combine(标题)
}
var id=UUID().uuistring
变量标题:字符串
var指令:字符串
var图像:图像
var步骤:Int
}
结构ContentView:View{
让checklistData=\[
检查表(标题:“水果”,说明:“1份vakje=1份100g的stuk”,图像:图像(“猕猴桃”),步骤:Int(4)),
检查表(标题:“水果”,说明:“1份vakje=1份100g的stuk”,图像:图像(“猕猴桃”),步骤:Int(2))
\]
var body:一些观点{
列表(checklistData,id:\.self){中的检查表
ChecklistView(检查表:检查表)
}
}
}

感谢您的快速回复!我收到以下错误:“使用未解析标识符“x”我以为你自己知道如何填写x,如果不知道,请向我们展示你所有的代码,以便我可以帮助你。嗨,克里斯,我已经添加了完整的代码。我是Swift的新手,所以仍然在了解很多东西。谢谢你的帮助!谢谢克里斯!我已经更新了上面的完整代码。仍然因为home view而出错。你知道发生了什么吗?非常感谢!!什么时候您单击一个加号,整行的加号将成为代码的复选标记。@JoëlvanBodegraven,您可能会遇到错误,因为在将此
ForEach(0..
添加到它工作的代码后,出现了额外的反斜杠!!谢谢大家!!
HStack {
       ForEach(0 ..< checklistData[x].steps) { index in
       VStack {
           Button(action: {
              self.checked[index].toggle()
                                })
struct ChecklistView: View {
    var checklist: Checklist
    @State var currentProgress: Float = 0.0
    @State var checked = \[false, false, false, false\]


    var body: some View {
        ZStack(alignment: .leading) {

            //            RoundedRectangle(cornerRadius: 20)
            //                .foregroundColor(.red).opacity(0.5)
            //                .frame(width: 200)

            VStack {
                HStack(alignment: .top) {
                    checklist.image
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 40, height: 40)
                        .padding(.trailing)
                    VStack(alignment: .leading) {
                        Text(checklist.title)
                            .font(.system(size: 16, weight: .medium))
                            .padding(.bottom, 4)

                        Text(checklist.instruction.uppercased()).font(.system(size: 12))

                        HStack {
                            ForEach(0 ..< checklist.steps) { index in
                                VStack {
                                    Button(action: {
                                        self.checked\[index\].toggle()
                                    }) {
                                        ZStack {

                                            RoundedRectangle(cornerRadius: 8)
                                                .foregroundColor(self.checked\[index\] ? Color("LightGreen") : .gray )
                                                .frame(width: 40, height: 40)

                                            Image(systemName: self.checked\[index\] ?  "checkmark" : "plus")
                                                .resizable()
                                                .aspectRatio(contentMode: .fit)
                                                .frame(width: 16, height: 16)
                                                .foregroundColor(self.checked\[index\] ? .white : .white)

                                        }
                                    }
                                }
                            }
                        }
                    }
                }.frame(width: 350, alignment: .leading)
            }
        }
        .frame(width: 350)
        .padding(.bottom, 16)
        .cornerRadius(8)
        .padding(.top, 20)
    }
}

// MARK: data checklist
struct Checklist: Identifiable, Hashable {

    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
        hasher.combine(title)
    }

    var id = UUID().uuidString
    var title: String
    var instruction: String
    var image: Image
    var steps: Int
}



struct ContentView: View {

    let checklistData = \[
        Checklist(title: "Fruit", instruction: "1 vakje = 1 stuk of 100g", image: Image("kiwi"), steps: Int(4)),
        Checklist(title: "Fruit", instruction: "1 vakje = 1 stuk of 100g", image: Image("kiwi"), steps: Int(2))

    \]

    var body: some View {
        List(checklistData, id: \.self) { checkList in
            ChecklistView(checklist: checkList)
        }
    }
}