I have a list with selecting rows and a search bar. Search bar freezes up the list, but I fixed that using id(UUID())
It created another problem, in which when user taps on a row, scroll jumps to top. Sometimes, when selecting few rows it crashes with this error: precondition failure: attribute failed to set an initial value: 96
struct ContentView: View {
@ObservedObject var viewModel = ViewModel()
@State private var searchText: String = ""
@State private var selected = Set<Model>()
var body: some View {
VStack {
SearchBar(text: $searchText, placeholder: "Search")
List(
viewModel.strings.filter({ searchText.isEmpty ? true : $0.title.lowercased().contains(searchText.lowercased()) })
, selection: $selected) { model in
MultipleSelectionRow(selectedItems: self.$selected, model: model)
}
.id(UUID()) /// This line causes strange behaviour.
}
}
}
The full project is available on GitLab with other screencasts and files like selection view, search bar and viewModel.

SOLVED:
There're initially two ways to use list, but each one had it's own bugs.
I ended up using UITableView with UIViewRepresentable. When I replaced List with UITableView, I added property @Binding var offset: CGFloat and updated it every time row is being selected:
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
offset = tableView.contentOffset.y
return indexPath
}
Sorry for unclear question, it was clear in my mind back then. Thank you for trying to help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With