Hi, I had same problem.
I worked around the problem by forcibly re-rendering the list.
(But the list may flicker when redrawing.)
I’ve marked the changes in the original sample code with 🌟.
I hope the problem will be fixed iOS 16.1...
Outline
Hide the list before updating data.
Show the list from DispatchQueue.main.async when data is updated.
Scroll by list.onAppear or list.onChange
import SwiftUI
///A simple data model for the demo. Only stores an UUID.
struct DataModel: Identifiable, Hashable {
let id: UUID = UUID()
var nameUUID: String {
id.uuidString
}
}
struct ContentView: View {
///Array with some data to show
@State private var data: [DataModel] = []
///Selected row
@State private var selection: DataModel?
// 🌟 In some situations, the initial value should be true.
@State private var isHidingList = false
var body: some View {
VStack(alignment: .leading) {
HStack {
//Create a new array for showing in the list.
//This array will be bigger than the last one.
//The selection will be the last element of the array (triggering the bug)
Button {
//Increment the size of the new List by 5
let numberElements = data.count + 5
//Create a new Array of DataModel with more 5 elements that the previous one
let newData = (0 ..< numberElements).map { _ in DataModel() }
//Select the last element of the array/list.
//This will make sure that the scrollTo will go to the end
let newSelection = newData.last
// 🌟 1. hide list before updating data.
//Update State for the new values
isHidingList = true
data = newData
selection = newSelection
} label: {
Text("Randomize & Select Last")
}
Spacer()
//Create a new array for showing in the list.
//This array will be bigger than the last one.
//The selection will be the a random element of the array (only triggering the bug when the element is )
Button {
//Increment the size of the new List by 5
//If empty will start with 40 (reducing the odds of triggering the bug)
let numberElements = data.count == 0 ? 40 : data.count + 5
//Create a new Array of DataModel with more 5 elements that the previous one
let newData = (0 ..< numberElements).map { _ in DataModel() }
//Select a random element of the array/list.
//This will scroll if the element is 'inside' the previous list
//Otherwise will crash
let newSelection = newData.randomElement()
// 🌟 1. hide list before updating data.
//Update State for the new values
isHidingList = true
data = newData
selection = newSelection
} label: {
Text("Randomize & Select Random")
}
}
.padding()
// 🌟
//MARK: ScrollViewReader and List
if isHidingList {
list.hidden()
} else {
list
}
}
// 🌟 2. Show the list from DispatchQueue.main.async when the data is updated.
.onChange(of: data) { _ in
DispatchQueue.main.async {
self.isHidingList = false
}
}
}
private var list: some View {
ScrollViewReader {
proxy in
List(data, selection: $selection) {
dataElement in
//Row (is only the UUID for the rows
Text(dataElement.id.uuidString)
.id(dataElement)
.tag(dataElement)
}
// 🌟3. Scroll
.onAppear() {
guard !isHidingList else { return }
if let selection {
proxy.scrollTo(selection)
}
}
// 🌟 This will not be called in this sample.
.onChange(of: data, perform: { newValue in
guard !isHidingList else { return }
if let selection {
proxy.scrollTo(selection)
}
})
}
}
}
Topic:
Programming Languages
SubTopic:
Swift
Tags: