0

我可以使用以下代码在 SwiftUI 中创建分组列表:

import SwiftUI

struct ContentView: View {
    var body: some View {
        List {
            Section(header: Text("Freyr / Freya's Aett")) {
                
            }
            Section(header: Text("Heimdall's Aett")) {
                
            }
            Section(header: Text("Tyr's Aett")) {
                
            }
            Section(header: Text("Additional Runes")) {
                
            }
        }.listStyle(GroupedListStyle())
    }
}

我有一个单独的 swift 文件,它将数据存储在数组中,但我无法将它导入到我的列表中,就像我在 Storyboards 中使用以下代码时那样。

import Foundation
import SwiftUI

struct Rune: Identifiable {
    var runeName: String
    var runeImage: String
    var runeDescription: String
    let id = UUID()
}

//MARK: - Runes Array Standard Orientation

var runeTitle = [["Freyr/Freya's Aett"], ["Heimdall's Aett"], ["Tyr's Aett"], ["Additional Runes"]]

let runesArray = [[Rune(runeName: "Fehu", runeImage: String(("Fehu")), runeDescription: "Description Goes Here")],

                   [Rune(runeName: "Hagalaz", runeImage: String(("Hagalaz")), runeDescription: "Description Goes Here")],

                  [Rune(runeName: "Tiwaz", runeImage: String(("Tiwaz")), runeDescription: "Description Goes Here")],

                  [Rune(runeName: "Blank Rune", runeImage: String(("Blank")), runeDescription: "Description Goes Here")]]
4

1 回答 1

1

您只需要一种方法来确定每个部分的内容。这种特殊的方式效率很低,因为它会遍历runesArrayfor each 部分中的每个项目,但您可以可视化正在发生的事情,然后处理您的数据。

import SwiftUI

struct SectionedList: View {
    var body: some View {
        List {
            Section(header: Text(runeTitle[0][0])) {
                ForEach(runesArray, id: \.id){rune in
                    if rune.runeTitle == runeTitle[0][0]{
                        Text(rune.runeName)
                    }
                }
            }
            Section(header: Text(runeTitle[1][0])) {
                ForEach(runesArray, id: \.id){rune in
                    if rune.runeTitle == runeTitle[1][0]{
                        Text(rune.runeName)
                    }
                }
            }
            Section(header: Text(runeTitle[2][0])) {
                ForEach(runesArray, id: \.id){rune in
                    if rune.runeTitle == runeTitle[2][0]{
                        Text(rune.runeName)
                    }
                }
            }
            Section(header: Text(runeTitle[3][0])) {
                ForEach(runesArray, id: \.id){rune in
                    if rune.runeTitle == runeTitle[3][0]{
                        Text(rune.runeName)
                    }
                }
            }
            //This is another way to do it.
            /*
            ForEach(0..<runeTitle.count){idx in
                Section(header: Text(runeTitle[idx][0])) {
                    
                    ForEach(runesArray, id: \.id){rune in
                        if rune.runeTitle == runeTitle[idx][0]{
                            Text(rune.runeName)
                        }
                    }
                }
            }
            */
        }.listStyle(GroupedListStyle())
    }
}
struct Rune: Identifiable {
    var runeName: String
    var runeImage: String
    var runeDescription: String
    var runeTitle: String
    let id = UUID()
}

//MARK: - Runes Array Standard Orientation

var runeTitle = [["Freyr/Freya's Aett"], ["Heimdall's Aett"], ["Tyr's Aett"], ["Additional Runes"]]

let runesArray = [Rune(runeName: "Fehu", runeImage: String(("Fehu")), runeDescription: "Description Goes Here", runeTitle: runeTitle[0][0]),
                  
                  Rune(runeName: "Hagalaz", runeImage: String(("Hagalaz")), runeDescription: "Description Goes Here", runeTitle: runeTitle[1][0]),
                  
                  Rune(runeName: "Tiwaz", runeImage: String(("Tiwaz")), runeDescription: "Description Goes Here", runeTitle: runeTitle[2][0]),
                  
                  
                  Rune(runeName: "Blank Rune", runeImage: String(("Blank")), runeDescription: "Description Goes Here", runeTitle: runeTitle[3][0])]

我添加runeTitle到你的Rune结构中。

于 2020-09-04T19:00:58.193 回答