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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.