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

Fussing with Table on MacOS

Forums > SwiftUI

I have a Table for a MacOS app using SwiftData and two questions.

One column uses a simple key value for the object name property and others use computed values. The Table uses DisclosureTableRow and ForEach and all is well. Parent values show the total sum of their own + those of all their children and the user can collapse and expand hierarchical groups.

What I'd like to add is a way to sense the expanded/collapsed state of a parent row so as to be able to show either just the parent's own value or the total sum. Is it possible to access the expansion state for each row which I could then use in an if statement in the TableColumn definition?

I'd also like to add some custom rows which use a custom name and custom value. This seems straighforward for the column with the key path to make a dummy name

TableRow(SwiftDataClass(name: "Custom Name"))

but not for the computed property columns. Is there another way to override the TableRow elements?


An answer to Question 1 is to use @Bindable on the SwiftData object then use isExpanded: $object.isExpanded in the DisclosureTableRow

In a nested DisclosureTableRow I use @Bindable in the ForEach: ...object in @Bindable var object = object then continue as before with the new bindable object

This way the object can report on its expanded state for programatic view changes, no @State var needed

This comes from https://developer.apple.com/documentation/swiftui/bindable/

As for Question 2, I just made some specific objects with the desired property values and used some if statements in the TableColumn description to customize what column data is displayed for those particular objects


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!

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.