SwiftUI - 다이나믹 List

김시온·2021년 5월 12일
0

Swift

목록 보기
8/8


import SwiftUI

struct ContentView: View {
    
    var articles: [Article] = [
        Article(title: "The comprehensive guide to flutter", image: "flutter-app", author: "ALEXAY", rating: 3, context: "There are many challenges in the software development, but there is one beast."),
        Article(title: "How to make swift button", image: "swiftui-button", author: "LEXY", rating: 4, context: "Hello and welcome to this tutorial!!"),
        Article(title: "Using swift protocols to manage app", image: "protocols", author: "JULIA", rating: 1, context: "I am the protocol king"),
    ]
    
    var body: some View {
        
        List {
            ForEach(articles) { article in
                VStack(alignment: .leading) {
                    Image(article.image)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .cornerRadius(10)
                    Text(article.title)
                        .font(.system(.largeTitle, design: .rounded))
                        .fontWeight(.black)
                    Text(article.author)
                        .font(.system(.body))
                        .foregroundColor(Color(.systemGray))
                    HStack {
                        ForEach(0..<article.rating) {_ in
                            Image(systemName: "star.fill")
                                .foregroundColor(Color(.systemYellow))
                                .font(.caption)
                        }
                    }
                    Text(article.context)
                        .font(.system(.body))
                        .foregroundColor(Color(.systemGray))
                        
                }
            }
        }
    }
    
}

struct Article: Identifiable {
    var id = UUID()
    var title: String
    var image: String
    var author: String
    var rating: Int
    var context: String
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

0개의 댓글