TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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?

2      

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

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.