Skip to content

Instantly share code, notes, and snippets.

@duanebester
Created January 28, 2020 01:30
Show Gist options
  • Save duanebester/aa64b76651caa4fb16114d74543b45fb to your computer and use it in GitHub Desktop.
Save duanebester/aa64b76651caa4fb16114d74543b45fb to your computer and use it in GitHub Desktop.
Song List View
import SwiftUI
struct Song: Identifiable, Codable {
let id: Int
let name: String
let artist: String
}
struct SongListView: View {
let names = ["Bailamos", "Dance Monkey", "One in a million", "Waka Waka", "Macarena"]
let artists = ["Duane", "Jared", "Zach", "Eduardo", "Miguel"]
func getData(number: Int = 20) -> [Song] {
var items: [Song] = []
for index in 1...number {
items.append(Song(id: index, name: names.randomElement()!, artist: artists.randomElement()!))
}
return items
}
var body: some View {
List {
ForEach(self.getData()) { song in
HStack {
Image("\((song.id % 6) + 1)")
.resizable()
.frame(width: 50, height: 50)
.clipped()
VStack(alignment: .leading) {
Text(song.name)
Text(song.artist).font(.caption)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment