UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: LazyVGrid and Array

Forums > SwiftUI

    let datas = ["a","a","a1","b","b43","b", "f", "g"]

    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
    ]

    var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(datas, id: \.self) { item in
                    Text(item)
                }
            }
            .padding(.horizontal)
        }
        .frame(maxHeight: 300)
  }

Dear community, this Code is the third example from the article "How to position views in a grid using LazyVGrid and LazyHGrid" from Hacking with swift. I change the array and make all GridItems flexible. All similar elements of the array datas are not shown. Here, the second a and the second b are not shown. The part of the table is shown, but it is empty. Why are elements are not shown? Thank you!

1      

try this , .self wants ForEach elements unique, which is not the case in your array, so we use this way of accesing indices and calling

 ScrollView {
                   LazyVGrid(columns: columns, spacing: 20) {
                       ForEach(datas.indices) { index in
                           let item = datas[index]
                           Text(item)
                       }
                   }
                   .padding(.horizontal)
               }
               .frame(maxHeight: 300)
    }

2      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.