Table view with dynamic content - is it possible?

Hi,

How can I use a Table view to build a data grid for a dynamic set of data?

Say, I have a 2D collection of data elements (a matrix, basically) with variable number of columns, and a 1D collection of column labels (a vector). For the sake of simplicity, let's assume that all data elements are Strings.

let data: [[String]]; let columnLabels: [String]

How can I build TableColumn definitions without key paths?

Thanks!

Do you want to do this in UIKit or in SwiftUI ?

Or is it MacOS ? In that case, that's much easier.

Multicolumns table are not easy with TableViews. You'd probably better use CollectionView.

See also if UIKit: https://developer.apple.com/documentation/uikit/uicollectionview?changes=_4

I was hoping to use the new Table view in SwiftUI on Mac OS.

@Claude31, you mentioned that it's much easier in MacOS? Do you mind sharing more details on this, please? Thanks!

In case anyone has the same question, I couldn't make the new TableView work with dynamic data source. Then I tried constructing a bunch of TextFields dynamically in a grid. It kind of worked, but it was very sluggish when scrolling. So I ended up wrapping NSTableView in NSViewRepresentable and working directly with NSTableView APIs. Performance is great with NSTableView and NSTextField as a cell view.

The function of SwiftUI Table component can hardly be extended. It is basically a toy.

It's now possible in SwiftUI with TableColumnForEach [1]

But don't use it.

I tried and I got crashes when reloading data using Observable, I got crashes when changing columns. My app hung and needed killing when switching away from the table view and back. It crashes if there are zero columns.

It's also really slow. Unusably so with about 200 rows and 150 columns. I tried with a release build and no difference was noticable.

NSTable on the other hand handles everything admirably.

[1] https://developer.apple.com/documentation/swiftui/tablecolumnforeach

Table view with dynamic content - is it possible?
 
 
Q