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

SOLVED: ForEach inside a Disclosure Group

Forums > SwiftUI

I'm trying to "hide" a long list of elements under DisclosureGroups, but I can't make them appear.

This shows all "Tutorands" inside the "ForEach" perfectly:

Section ("Tutorands"){
          ForEach(tutorOf) { student in
            HStack {
              student.photoAsImage
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 40, height: 40)
                .clipShape(Circle())
                .shadow(radius: 10)
              VStack (alignment: .leading){
                Text(student.name ?? "")
                Text(student.course?.level ?? "")
                  .foregroundColor(.secondary)
              }
            }
            .tag(student as Student?)
          }
        }

But this "equivalent" with disclosure code shows only the title and icon of DisclosureGroup: no data when tapped.

DisclosureGroup (content: {
          ForEach(tutorOf) { student in
            HStack {
              student.photoAsImage
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 40, height: 40)
                .clipShape(Circle())
                .shadow(radius: 10)
              VStack (alignment: .leading){
                Text(student.name ?? "")
                Text(student.course?.level ?? "")
                  .foregroundColor(.secondary)
              }
            }
            .tag(student as Student?)
          }
        }, label: {
          Label {
            Text("Tutorands")
          } icon: {
            Image(systemName: "person.2.fill")
              .foregroundColor(.accentColor)
          }
        })

Am I missing something? Is this the correct way of doing it?

Thanks in advance.

1      

Put your ForEach inside a VStack.

Maybe a List would work too; I dunno.

1      

Yes! That worked! I think I tried this kind of solution, but anyway now it works: with VStack inside.

Thanks!

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.