Swift Foreach具有非恒定范围视图刷新

发布于 2025-01-29 23:32:27 字数 1810 浏览 4 评论 0 原文

我知道这是一个简单的问题,但我没有找到答案。我想了解基本概念。

我正在尝试更新一个具有非恒定范围的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()
    }
}

I know it's a simple question, but I haven't found an answer. I want to understand the underlying concept.

I'm trying to update a ForEach with a non constant range, the closing parameter is a variable that is assigned to a button.

The variable is assigned with a @State so it's supposed to refresh the view. Somehow it's not working.



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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2025-02-05 23:32:27

问题在于未识别范围。让我们进行一些行

    struct Row: Identifiable {
        let id = UUID()
    }

,然后设置一个可识别项目的数组,

    @State private var numberOfTimes = 5
    @State private var rows = Array(repeating: Row(), count: 5)

现在您可以有一个响应列表

    List{
        ForEach(rows) { row in
            Text("Dynamic row")
        }
    }

调用“更改更新更新”以重新创建阵列

    .onChange(of: numberOfTimes) { newValue in
        rows = Array(repeating: Row(), count: newValue)
        numberOfTimes = newValue
    }

应在表单上调用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

    struct Row: Identifiable {
        let id = UUID()
    }

Then set up an array of identifiable items

    @State private var numberOfTimes = 5
    @State private var rows = Array(repeating: Row(), count: 5)

Now you can have a responsive list

    List{
        ForEach(rows) { row in
            Text("Dynamic row")
        }
    }

Call the on change update to recreate the array

    .onChange(of: numberOfTimes) { newValue in
        rows = Array(repeating: Row(), count: newValue)
        numberOfTimes = newValue
    }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文