2

I have a array of string

fakeData = ["Array A",
            "Array B",
            "Array C",
            "Array D"]

How Can I implement this array in ListView in SwiftUI ?

4

1 回答 1

5

首先,您应该标记数组,就State好像您要更改视图内的数据一样,或者不理会它,但我选择State演示:

@State var fakeData = ["Array A", "Array B", "Array C", "Array D"]

然后只用一行代码从中列出一个列表:

List(fakeData, id:\.self) { Text($0) }

包起来:

struct ContentView: View {

    @State var fakeData = ["Array A", "Array B", "Array C", "Array D"]

    var body: some View {
        List(fakeData, id:\.self) { Text($0) }
    }
}
于 2019-11-07T19:29:08.253 回答