swift - Use data to define amount of objects in Array
问题描述
I'm working on a checklist in SwiftUI and want to define the amount of checkboxes in my data. So for example, in case of fruit, the user should tick 3 checkboxes whereas water requires more checks because you need to drink more glasses of water a day.
I'm using "for each" in a HStack to make an array of checkboxes:
HStack {
ForEach(0 ..< 3) { index in
VStack {
Button(action: {
self.checked[index].toggle()
})
I want to replace the ( 0..<3) with checklist.steps which is where the amount (INT) is defined.
let checklistData = [
Checklist(title: "Fruit", instruction: "1 vakje = 1 stuk of 100g", steps: Int(4)),
Is there a way to define the amount of checkboxes in my data?
Thanks in advance <3
Solution
The following code worked for me:
HStack {
ForEach(0 ..< checklist.steps) { index in
VStack {
Button(action: {
self.checked[index].toggle()
})
解决方案
我不确定你是否想要这个?
HStack {
ForEach(0 ..< checklistData[x].steps) { index in
VStack {
Button(action: {
self.checked[index].toggle()
})
推荐阅读
- python - Python值错误:解包的值太多?
- django - 链接css样式表django python3?
- javascript - XMLHttp 请求帖子没有点击 URL,也没有显示任何错误
- nginx - 将codebuild连接到jenkins master时如何解决“无法找到请求目标的有效认证路径”
- python - 将字符串转换为字符串列表的最佳方法是什么
- string - 如何在bash中获取冒号前的最后一个值
- html - 返回屏幕时数据消失
- javascript - 为什么我会收到“UnhandledPromiseRejectionWarning”,这是什么意思?
- java - Java 排序,默认排序功能仅工作
- ruby - Sorbet:实例变量必须在`initialize`中声明