繁体   English   中英

在 Picker 中使用 ForEach

[英]Using ForEach inside a Picker

我在使用 SwiftUI 将数据从数组中提取到选择器时遇到问题。 我可以正确地列出我感兴趣的数据,但似乎无法使相同的逻辑工作以将数据拉入选择器。 我已经用几种不同的方式对其进行了编码,但是我目前的方式给出了这个错误:


Referencing initializer 'init(_:content:)' on 'ForEach' requires that 'Text' conform to 'TableRowContent'

代码如下:

import SwiftUI

struct BumpSelector: View {
    @ObservedObject var model = ViewModel()
    @State var selectedStyle = 0
        
    init(){
        model.getData2()}
    
    
    var body: some View {
        VStack{
            List (model.list) { item in
                Text(item.style)}
           
            Picker("Style", selection: $selectedStyle, content: {
                ForEach(0..<model.list.count, content: { index in
                    Text(index.style)
                           })
                       })
            }
}

model 在这里:

import Foundation

struct Bumps: Identifiable{
    var id: String
    var style: String
}

ViewModel 在这里:

import Foundation
import Firebase
import FirebaseFirestore

class ViewModel: ObservableObject {
    @Published var list = [Bumps]()
    @Published var styleArray = [String]()
   
    func getData2() {
           
        let db = Firestore.firestore()
        db.collection("bumpStop").getDocuments { bumpSnapshot, error in

            //Check for errors first:
            if error == nil {

                //Below ensures bumpSnapshot isn't nil
                if let bumpSnapshot = bumpSnapshot {

                    DispatchQueue.main.async {

                        self.list = bumpSnapshot.documents.map{ bump in

                            return Bumps(id: bump.documentID,
                                         style: bump["style"] as? String ?? "")
                        }
                    }
                }
            }

            else {
                //Take care of the error
            }
        }

        }
}

ForEach中的index只是一个Int ,没有与Int关联的样式。 您可以尝试这种方法来使Picker与其ForEach一起工作:

struct BumpSelector: View {
    @ObservedObject var model = ViewModel()
    @State var selectedStyle = 0
    
    init(){
        model.getData2()
    }
    
    var body: some View {
        VStack{
            List (model.list) { item in
                Text(item.style)}
            
            Picker("Style", selection: $selectedStyle) {
                ForEach(model.list.indices, id: \.self) { index in
                    Text(model.list[index].style).tag(index)
                }
            }
            
        }
    }
}

编辑-1:

Text(model.list[selectedStyle].style)将为您提供selectedStyle所需的style 但是,在使用索引时,您需要确保它在使用时是有效的。 也就是说,使用if selectedStyle < model.list.count { Text(model.list[selectedStyle].style) }

您也可以使用这种不使用index的替代方法:

 struct Bumps: Identifiable, Hashable {   // <-- here
     var id: String
     var style: String
 }

 struct BumpSelector: View {
     @ObservedObject var model = ViewModel()
     @State var selectedBumps = Bumps(id: "", style: "") // <-- here
     
     init(){
         model.getData2()
     }
     
     var body: some View {
         VStack{
             List (model.list) { item in
                 Text(item.style)
             }
             Picker("Style", selection: $selectedBumps) {
                 ForEach(model.list) { bumps in
                     Text(bumps.style).tag(bumps)  // <-- here
                 }
             }
         }
         .onAppear {
             if let first = model.list.first {
                 selectedBumps = first
             }
         }
     }
 }
 

然后使用selectedBumps ,就像任何Bumps一样,例如selectedBumps.style

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM