I'm trying out the new Table to show some sorted data with a single column, but I'm having problems with it not updating when items go out of sight. Swapping it out with a simple List fixes it weirdly enough.
Reproduction:
- Ensure that macOS Montery (beta 1 or 2) and Xcode 13 Beta 2 are installed
- Create a new blank SwiftUI project targeting macOS, with its deployment target set to 12.0
- Paste the following code:
struct ContentView: View {
@State var data: [TestStruct] = []
var sortedData: [TestStruct] {
return data.sorted(by: { $0.text < $1.text })
}
var body: some View {
Table(sortedData) { // Swapping this with a List fixes everything
TableColumn("Text", value: \.text)
}
.toolbar {
Button(action: {
addItem(longString: false)
}) {
Text("Add Short")
}
Button(action: {
addItem(longString: true)
}) {
Text("Add Long")
}
}
}
private func addItem(longString: Bool) {
withAnimation {
data.append(TestStruct(text: longString ? "This is long so you can differentiate better" : "Short"))
}
}
}
struct TestStruct: Identifiable {
var text: String
var id = UUID()
}
- Run the app, and press the "Add Long" button. This simply adds an item that has a long string.
- Without scrolling, spam click the "Add Short" button. Add enough items so that the long string you added previously goes out of the window, plus a few more items.
- Once finished, scroll through the table, and you should see there are duplicate long strings, even though you only added one
What is causing this issue? Swapping the Table with a List fixed the problem entirely, but I would like to keep the Table (so I can add more columns later).
Also, is anybody else able to reproduce this issue? I've been able to reproduce on both versions of macOS Montery (beta 1 and 2), with Xcode 13 Beta 2