I have created the following struct and here's a short example of my array that I wish to be used in a list in SwiftUI:
import Foundation
import SwiftUI
struct Rune: Identifiable {
var runeName: String
var runeImage: String
var runeDescription: String
let id = UUID()
}
let runesArray = [Rune(runeName: "Fehu", runeImage: String(("Fehu.png")), runeDescription: "(Description Goes Here"),
Rune(runeName: "Uruz", runeImage: String(("Uruz.png")), runeDescription: "(Description Goes Here"),
Rune(runeName: "Thurisaz", runeImage: String(("Thurisaz.png")), runeDescription: "(Description Goes Here"]
My images do not show up when I use the following code but my text runeName text runeDescription appears as expected when I click on the row and goto my DetailView and I can't figure out why:
ContentView:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List(runesArray) { rune in
NavigationLink(destination: DetailView(rune: rune)) {
RuneRow(rune: rune)
}
}
.navigationBarTitle("Rune Companion")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct RuneRow: View {
let rune: Rune
var body: some View {
HStack {
Image(rune.runeImage)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 60, height: 60)
Spacer()
Text(rune.runeName)
.font(.title)
.fontWeight(.bold)
Spacer()
}
}
}
DetailView:
import SwiftUI
struct DetailView: View {
let rune: Rune
var body: some View {
NavigationView {
VStack {
Spacer()
Image(rune.runeImage)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 200, height: 200)
Spacer()
Text(rune.runeDescription)
.padding(.horizontal)
Spacer()
}
.navigationBarTitle(rune.runeName)
.navigationBarTitleDisplayMode(.large)
}
}
}
struct DetailView_Previews: PreviewProvider {
static var previews: some View {
DetailView(rune: runesArray[0])
}
}
When I change Image(rune.runeImage)
to Image("Fehu")
the image for Fehu does appear but populates all three rows as it should do, showing that my images can be loaded successfully. Any ideas what I've done wrong?
You don't need to specify an extension ( .png
) for your images.
Replace:
runeImage: String(("Fehu.png"))
with:
runeImage: "Fehu"
when you create Runes in runesArray
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.