簡體   English   中英

如何在SwiftUI中將真實數據(而非testdata)附加到列表?

[英]How to attach real data (not testdata) to a list in SwiftUI?

我正在使用帶有ContentView和DetailView的SwiftUI創建一個相當簡單的列表應用程序。

但是,在我看到的所有教程中,傳遞給兩個視圖的數據源只是“ testData”,而數據源僅在#if DEBUG部分中定義。

如何在ContentView本身中定義應從哪個數組加載數據?

我已經創建了多個數據數組,但是我不知道在#if DEBUG之外的地方實際可以訪問此數據。

import SwiftUI

 struct ContentView : View {
    @ObjectBinding var store = GrapeStore()

    var body: some View {
        NavigationView {
            List {
                ForEach(store.grapes) { grape in
                GrapeCell(grape: grape)
                }
        }
        .navigationBarTitle("Wine grapes")
        .navigationBarItems(trailing: EditButton())
        .listStyle(.grouped)
    }

}

}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
    Group {
        ContentView(store: GrapeStore(grapes: AGrapes))

        ContentView(store: GrapeStore(grapes: testData))
            .environment(\.colorScheme, .dark)

    }
}
}
#endif

struct GrapeCell : View {
let grape: Grape

var body: some View {
    return NavigationLink(destination: GrapeDetail(grape: grape)) {
        Image(systemName: "photo")

        VStack(alignment: .leading) {
            Text(grape.name)
            Text("\(grape.type) grape")
                .font(.subheadline)
                .foregroundColor(.secondary)
        }
    }
}
}

我設法從ACGrapes和testData數組中提取數據,但這僅在預覽中。 如何確定應實際提取哪些數據?

您還必須從項目的場景委托文件中傳遞數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM