TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: How to loop a View() using ForEach?

Forums > SwiftUI

Hi! I'm a newbie and I have this card created called CardView().

struct CardView: View {
    // MARK: - PROPERTIES
    var brewery:Brewery

    // MARK: - CONTENT
    var body: some View {
        VStack {
            ZStack (alignment: .bottom) {
                Image(brewery.cardImage)
                    .resizable()
                Rectangle ()
                    .fill(LinearGradient(gradient: Gradient(colors: [Color.black, Color.clear]), startPoint: .bottom, endPoint: .top))
                    .padding(.top, 90)
                VStack {
                    HStack {
                        Spacer()
                        Image(systemName:"heart")
                            .font(.system(size: 22, weight: .light))
                            .foregroundColor(Color.white)
                            .padding()
                    }
                    Spacer()
                    VStack (spacing: 5) {
                        Text(brewery.breweryName.uppercased())
                            .font(.system(size: 26, weight: .black))
                            .foregroundColor(Color("ColorLight"))
                        HStack (spacing: 15) {
                            VStack (spacing: 4) {
                                Text("BEER EXPERIENCE")
                                    .font(.footnote)
                                    .foregroundColor(Color.white)
                                StarsView(brewery: brewery, star: brewery.overallRatingBeer)
                            }
                            VStack (spacing: 4) {
                                Text("AMBIENCE")
                                    .font(.footnote)
                                    .foregroundColor(Color.white)
                                StarsView(brewery: brewery, star: brewery.overallRatingAmbience)
                            }
                        }
                    }
                }.padding(.bottom, 15)
            }
            .frame(height: 180)
            .cornerRadius(10)
        }
        .padding(.horizontal, 10)
    }
}

Inside another view I want repeat it vertically. I tried like this but no success. What am I missing and don't understand?

struct CardScrollView: View {
    // MARK: - PROPERTIES
    var brewery:Brewery

    // MARK: - CONTENT
    var body: some View {
        VStack (alignment: .center, spacing: 5) {
            ForEach(self.brewery, id:\.id) { _ in
                CardView(brewery: brewery)
            }
        }
    }
}

2      

brewery in your CardScrollView seems to be a single item. ForEach needs something to loop through, like an array of Brewery objects.

So you need something like:

var breweries: [Brewery] = // however you get your Brewery objects

and then loop through self.breweries.

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.