Swiftui Table statement conditional table columns

Is it possible to have conditional table columns for a swifui Table statement?

Like for this code

                        TableColumn("Image") { artPiece in
                            if let imageData = artPiece.artImage.first, let image = UIImage(data: imageData!) {
                                Image(uiImage: image)
                                    .resizable()
                                    .frame(width: 50, height: 50)
                            } else {
                                Image(systemName: "photo")
                                    .resizable()
                                    .frame(width: 50, height: 50)
                            }
                        }
                        .customizationID("Image")
                        TableColumn("Name", value: \.artName)
                            .customizationID("Name")
                        TableColumn ("Art ID", value: \.artPieceID) { artPiece in
                            Text(String(artPiece.artPieceID))
                        }
                            .customizationID("Art ID")

have a conditional TableColumn for this part of my SWIFTDATA model var artDefinedFields: [ArtDefinedFields] = [] or if I change the variable string array to this var artDefinedFields: [ArtDefinedFields] = Array(repeating: ArtDefinedFields(), count: 10), initialize the array with "None" and only create a TableColumn when there is aArtDeginedFields value other than "None"

Answered by Claude31 in 800085022

You don't show the complete code, so harder to explain…

But the way I would do it would be to have the condition in the dataSource through a computed var.

Accepted Answer

You don't show the complete code, so harder to explain…

But the way I would do it would be to have the condition in the dataSource through a computed var.

So here is all the code

                    Table(artViewModel.filteredArtPieces, selection: $selection, sortOrder: $sortOrder, columnCustomization: $columnCustomization) {
                        TableColumn("Image") { artPiece in
                            if let imageData = artPiece.artImage.first, let image = UIImage(data: imageData!) {
                                Image(uiImage: image)
                                    .resizable()
                                    .frame(width: 50, height: 50)
                            } else {
                                Image(systemName: "photo")
                                    .resizable()
                                    .frame(width: 50, height: 50)
                            }
                        }
                        .customizationID("Image")
                        TableColumn("Name", value: \.artName)
                            .customizationID("Name")
                        TableColumn ("Art ID", value: \.artPieceID) { artPiece in
                            Text(String(artPiece.artPieceID))
                        }
                            .customizationID("Art ID")
                        TableColumn ("Price", value: \.artPrice) { artPiece in
                            Text (formatMoneyDouble(artPiece.artPrice))
                        }
                        .customizationID("Price")
                        TableColumn ("Date", value: \.artcreateDate) { artPiece in
                            Text (artPiece.artcreateDate, style: .date)
                        }
                   //     .resizable()
                        .customizationID("Date")
                      TableColumn("Artist", value: \.artistName)
                            .customizationID("Artist")
                        TableColumn("Meduim", value: \.artMedium)
                            .customizationID("Meduim")
                        TableColumn("Type", value: \.artType)
                            .customizationID("Type")
                        TableColumn("Status", value: \.artStatus)
                            .customizationID("Status")
                        TableColumn("Location", value: \.artLocation)
                            .customizationID("Location")
                   
              /*         I want to create columns for this variable     
var artDefinedFields: [ArtDefinedFields] = []  
which is defined in the datasource . */

                    }

                    .tableStyle(.inset)

struct ArtDefinedFields: Identifiable, Codable, Hashable { var id = UUID() var name: String var value: String var isVisible: Bool }

Ended up using a computed variable as Claude31 suggested. Worked great!

Swiftui Table statement conditional table columns
 
 
Q