I am using SwiftUI's Table to generate a table for a macOS app. I would like to be able to hide columns based on AppStorage entries. TableColumnBuilder doesn't appear to support conditional statements:
Table(tableData) {
// This does not work:
if showColumndA {
TableColumn("Column A", value: \Row.columnA)
}
// Other columns genereated here
// ...
}
Is there a way to build a table with SwiftUI where the user can choose what columns to show?
(Showing different tables based on all possible permutations for what columns are visible does not work for my situation)
As mentioned by Geoff Hackworth there appears to be a solution for macOS 14 (which was not an option for me at the time of this writing).
I did end up solving my problem by extending
@TableColumnBuilderto support conditionals (thanks Swift Lee for your amazing writeup of the@resultBuilderAPI):The following little example app uses my
@TableColumnBuilderextension to randomly show either Column A or Column B: