SwiftUI TableColumn with Custom Widget not able to Bind

Hello, everyone. I am having some troubles fixing up a UI in a mac app of mine, that just looks horrible for my liking, seeing as I was able to get closer to the interface I wanted with GTK than SwiftUI.

When I go looking through the docs, I see that I can get 2 of the three specific types of columns I need, which are as follows:

  • 1 column where cells are date pickers
  • 1 column with check boxes
  • 1 column with a combo box

As this application is supposed to be a checkbook ledger app that can import data from QIF and JSON, and has been registered in a way that users can import data on iOS directly via Air Drop, there will be a column that cannot be edited, but all other fields are editable, with the combo box column allowing users to enter in a new category type or selecting an existing category.

However, my problem right now is when I go to implement either or the two easiest columns to think about, I cannot seem to access the value I need because the closure, which looks like this:

TableColumn("Date", value: \Record.event.date) { record in
        DatePicker("", selection: $record.event.date, displayedComponents: [ .date])
      }

Says that record is not found in scope, but seeing as the record variable is supposed to refer to RowValue, according to this and many of the other initializers for a column with dates, I should not be getting this error.

When I actually go about to supply a closure for the content argument, the autocompletion in XCode gives me an underscore, which makes it difficult to create a binding because I have no way to access the date.

How should I go about fixing this?

Answered by brycec in 712637022

I got a reply to my query over on Discord regarding the subject of this thread, and it looks like what I want to do is not possible with SwiftUI at the moment, with the slight suggestion that checkboxes are, due to the video lesson shared here early.

The big hurdle in this is that Bindings are next to impossible in the given situation, which date pickers require, and likely more so with a ForEach in a lazy grid.

As such, I think I am going to mark this as solved, in spite of the situation likely not being fixable until the TableColumn API is refined or a real fix is implemented.

Using a TableColumn requires a Table {// content } rows: { // data source } pattern similar to below. Where in the example rowItems is a collection of Items for the data source.

Items

struct Item {
    let from: Date
    let to: Date
    let hours: Double
}

// The table view
Table(selection: $selection, sortOrder: $sortOrder) {
            // Sort column on \.from key value path of item
            TableColumn("From", value:\.from) { item in
                Text(item.from(date: .numeric, time: .omitted))
            }
             // Sort column on \.to key value path of item
            TableColumn("To", value:\.to) { item in
                Text(item.to.formatted(date: .numeric, time: .omitted))
            }
             // Sort column on \.hours key value path of item
            TableColumn("Total", value:\.hours) { item in
                Text(item.hours)
            }
        } rows: {
            // Populate the rows data source with TableRow types containing an item type from the rowItems collection which is passed on each TableColumm.
            ForEach(rowItems) { item in
                TableRow(item)
            }
        }

See Apple example code here: https://developer.apple.com/documentation/swiftui/building_a_great_mac_app_with_swiftui

Accepted Answer

I got a reply to my query over on Discord regarding the subject of this thread, and it looks like what I want to do is not possible with SwiftUI at the moment, with the slight suggestion that checkboxes are, due to the video lesson shared here early.

The big hurdle in this is that Bindings are next to impossible in the given situation, which date pickers require, and likely more so with a ForEach in a lazy grid.

As such, I think I am going to mark this as solved, in spite of the situation likely not being fixable until the TableColumn API is refined or a real fix is implemented.

SwiftUI TableColumn with Custom Widget not able to Bind
 
 
Q