<!--
{
  "availability" : [
    "iOS: 17.4.0 -",
    "iPadOS: 17.4.0 -",
    "macCatalyst: 17.4.0 -",
    "macOS: 14.4.0 -",
    "visionOS: 1.1.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/TableColumnForEach",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI18TableColumnForEachV"
  },
  "title" : "TableColumnForEach"
}
-->

# TableColumnForEach

A structure that computes columns on demand from an underlying collection of
identified data.

```
nonisolated struct TableColumnForEach<Data, ID, RowValue, Sort, Content> where Data : RandomAccessCollection, ID : Hashable, RowValue == Content.TableRowValue, Sort == Content.TableColumnSortComparator, Content : TableColumnContent
```

## Overview

Use `TableColumnForEach` to create columns based on a
<doc://com.apple.documentation/documentation/Swift/RandomAccessCollection>
of some data type. Either the collection’s elements must conform to
<doc://com.apple.documentation/documentation/Swift/Identifiable> or you
need to provide an `id` parameter to the `TableColumnForEach` initializer.

The following example shows the interface for an `AudioSampleTrack`, which h
as a collection of `AudioSample` across a dynamic number of `AudioChannel`s.
The `Table` is created for displaying rows for each sample. It has one
static column for the sample’s timestamp and uses a `TableColumnForEach`
instance to produce a column for each of the audio channels in the track.

```
struct AudioChannel: Identifiable {
    let name: String
    let id: UUID
}

struct AudioSample: Identifiable {
    let id: UUID
    let timestamp: TimeInterval
    func level(channel: AudioChannel.ID) -> Double
}

@Observable
class AudioSampleTrack {
    let channels: [AudioChannel]
    var samples: some RandomAccessCollection<AudioSample> { get }
}

struct ContentView: View {
    var track: AudioSampleTrack

    var body: some View {
        Table(track.samples) {
            TableColumn("Timestamp (ms)") { sample in
                Text(sample.timestamp, format: .number.scale(1000))
                    .monospacedDigit()
            }
            TableColumnForEach(track.channels) { channel in
                TableColumn(channel.name) { sample in
                    Text(sample.level(channel: channel.id),
                         format: .number.precision(.fractionLength(2))
                    )
                    .monospacedDigit()
                }
                .width(ideal: 70)
                .alignment(.numeric)
            }
        }
    }
}
```

## Topics

### Creating the collection

[`init ( _ : content :)`](/documentation/SwiftUI/TableColumnForEach/init(_:content:))

Creates an instance that uniquely identifies and creates table columns
across updates based on the identity of the underlying data.

[`init(_:id:content:)`](/documentation/SwiftUI/TableColumnForEach/init(_:id:content:))

Creates an instance that uniquely identifies and creates table columns
across updates based on the provided key path to the underlying data’s
identifier.

### Accessing collection content

[`content`](/documentation/SwiftUI/TableColumnForEach/content)

A function to create content on demand using the underlying data.

[`data`](/documentation/SwiftUI/TableColumnForEach/data)

The collection of underlying identified data that SwiftUI uses to create
columns dynamically.

## Relationships

### Conforms To

[`TableColumnContent`](/documentation/SwiftUI/TableColumnContent)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)