Swift Foreach具有非恒定范围视图刷新
我知道这是一个简单的问题,但我没有找到答案。我想了解基本概念。
我正在尝试更新一个具有非恒定范围的foreach,关闭参数是分配给按钮的变量。
该变量用@State分配,因此应该刷新视图。不知何故它不起作用。
import SwiftUI
struct ContentView: View {
@State private var numberOfTimes = 5
let timesPicker = [2,5,10,12,20]
@State private var tableToPractice = 2
enum answerState {
case unanswered
case wrong
case right
}
func listRange(){
}
var body: some View {
NavigationView{
HStack{
VStack{
Form{
Section {
Picker("Tip percentage", selection: $numberOfTimes) {
ForEach(timesPicker, id: \.self) {
Text($0, format: .number)
}
}
.pickerStyle(.segmented)
} header: {
Text("How many times do you want to practice?")
}
Section{
Stepper("Table to practice: \(tableToPractice.formatted())", value: $tableToPractice, in: 2...16 )
}
Button("Start Now", action: listRange).buttonStyle(.bordered)
List{
ForEach(0..<numberOfTimes){
Text("Dynamic row \($0)")
}
}
}.foregroundColor(.gray)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于未识别范围。让我们进行一些行
,然后设置一个可识别项目的数组,
现在您可以有一个响应列表
调用“更改更新更新”以重新创建阵列
应在表单上调用OnChange。
当您拥有更好的查看模型数据时,这将更有意义,请参见Apple文档以获取更深入的示例。
这是针对Lazy V堆栈的,但是数据模型设置是我想的
https://developer.apple.com/documentation/swiftui/grouping-data-with-with-lazy-stack-views
The problem is that the range is not identified. Lets make some rows
Then set up an array of identifiable items
Now you can have a responsive list
Call the on change update to recreate the array
the onChange should be called on the Form.
This will make more sense when you have better view model data, see apple documentation for a more in depth example.
This is for lazy v stack, but the data model setup is what I'm thinking of
https://developer.apple.com/documentation/swiftui/grouping-data-with-lazy-stack-views