美文网首页
SwiftUI—使用section将列表分为几个组

SwiftUI—使用section将列表分为几个组

作者: anny_4243 | 来源:发表于2020-07-16 13:11 被阅读0次

    原文链接:https://github.com/fzhlee/SwiftUI-Guide#-%E7%AC%AC14%E8%8A%82form-segment-

    当列表里的数据很多,并且这些数据可以被分组时,我们可以使用section,将列表分为几个组。

    示例代码:

    struct ContentView : View {
        private var languages = ["Swift", "Objective-C"]
        @State private var selectedLanguage = 0
        @State var workingYear: Double = 2
        @State var enableNotification = false
        var body: some View {
            NavigationView {
                Form {
                    Section(header: Text("Please enter your information:"), footer: Text("Note: Enabling notification to get more infomration")) {
                        Picker(selection: $selectedLanguage, label: Text("Languages")) {
                           ForEach(0 ..< languages.count) {
                            Text(self.languages[$0]).tag($0)
                           }
                        }.pickerStyle(SegmentedPickerStyle())
                        HStack{
                            Text("Working years")
                            Slider(value: $workingYear, in: 1...10, step: 1)
                        }
                        
                        Toggle(isOn: $enableNotification) {
                            Text("Enable Notification")
                        }
                    }
                    Button(action: {
                    // activate theme!
                        print("Your programming language: \(self.languages[self.selectedLanguage])")
                        print("Your working years: \(Int(self.workingYear))")
                        print("Enable notification: \(self.enableNotification)")
                    }) {
                        Text("Submit")
                    }
                }.navigationBarTitle(Text("Profiles"))
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:SwiftUI—使用section将列表分为几个组

          本文链接:https://www.haomeiwen.com/subject/exlphktx.html