Deselecting SwiftUI table rows ends up only deselecting visible rows??

Folks,

I have a SwiftUI view with a fairly simple table, nothing fancy:

Table(itemsToDisplay, selection: $selectedLines, sortOrder: $sortOrder) 
	{…}

Then I have created a Deselect all command added in the Edit menu, which toggles a boolean state variable called deselect, and I've written this modifier:

.onChange(of: deselect) {_ in selectedLines = Set<TableEntry.ID>(); deselect = false}

While this correctly deselects all visible rows, it turns out the hidden rows are not deselected and show up highlighted when the table is scrolled up or down. Is that the expected behaviour? How am I supposed to deselect all rows, including the invisible ones?

Thanks a bunch! V.

P.S: I’m using the latest MacOS 13 beta SDK with the latest Xcode beta (14.1ß3)

  • Darn, I just realised the 'deselect = false' part can trigger a recursive call. I’m not sure that’s what causes the problem, though

  • No, it is not. Removing that part has no effect.

  • Same behaviour if the deselect state variable is transferred into an observed object. Looks like a caching side-effect.

Replies

Well, this is getting worse by the day. Now it crashes with errors like [General] NSTableView error inserting/removing/moving row 64 (numberOfRows: 2).

The contents of the table change dynamically according to filters (like, say, a search regex pattern). I wonder if that’s what causes the problem. I tried to synchronise every change in contents to the main thread to not interfere with view updates, but to no avail. I also have those purple messages about publishing updates within view updates, but the stack doesn’t show any of my user code, as if it was inside the underlying Cocoa library itself.