You don't use TableColumnBuilder
like that; it's an attribute on a parameter like ViewBuilder
, not something you call directly.
Here, for instance, is a simplicfication of the declaration for the Table
initializer you are using in your first code snippet:
init<Data>(_ data: Data, @TableColumnBuilder<Value, Never> columns: () -> Columns)
//I removed the bits about generic conformances to make it easier to read
In your first snippet, everything between the {
and }
is the closure you are passing to the columns
parameter, which is marked as being a TableColumnBuilder
.
You can see that the columns
parameter is marked as being a TableColumnBuilder
. This is exactly parallel to how you use, say, VStack
to create a View
. Here is the initializer for VStack
:
init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, @ViewBuilder content: () -> Content)
See that @ViewBuilder
attribute? You don't call ViewBuilder
directly, just like for a Table
you don't call TableColumnBuilder
directly.
So just like how VStack
uses a closure parameter marked as being a ViewBuilder
to return View
s, a Table
uses a closure parameter marked as being a TableColumnBuilder
to return TableColumn
s.
Unfortunately, I am not running Monterey and therefore can't play around with the Table
API, so I can't answer your question about applying colors to the values in a TableColumn
.
However, looking at Apple's sample code and documentation, I imagine it would be something like this:
Table(viewModel.listOfStructs) {
TableColumn("Column Head", value: \ViewModel.columnValue) { rowValue in
Text(rowValue.columnValue)
.foregroundColor(.purple)
}
.width(min: 50, ideal: 55, max: nil)
}
In other words, what you have works fine to just display a String
but if you want to style it, you need to explicitly pass in a ViewBuilder
closure for the content
parameter of the TableColumn
.