NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

Create an expandable list out of two arrays

Forums > SwiftUI

I have these two arrays:

var customers = ["Example", "another"]
var projects = [["Website", "Example", "0:34:23"], ["Logo", "another", "0:58:56"], ["Big Project", "another", "5:23:56"]]

How can I make out of this an expandable list, which has in this case two categories "Example" and "another" and the children are the projects where the second item matches the customer.

The endresult should look like this:


Not sure but this might be what you looking for

struct Data {
    var text: String
    var description: [Data]?

struct ContentView: View {
    let data = [
        Data(text: "Example", description: [Data(text: "Website"), Data(text: "Example"), Data(text: "0:34:23")]),
        Data(text: "Another", description: [Data(text: "Logo"), Data(text: "another"), Data(text: "0:58.56")]),
        Data(text: "Project", description: [Data(text: "Big Project"), Data(text: "another"), Data(text: "5:23:56")])


    var body: some View {
        List(data, id:\.text, children: \.description) { item in
            if item.description != nil {
            } else {

Give this

or you can look at How to hide and reveal content using DisclosureGroup


Ok thanks i will look at it tomorrow. (I live in Germany)


Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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

Reply to this topic…

You need to create an account or log in to reply.

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.