Why the selected rows don't stay selected after scrolling in SwiftUI?

215 views Asked by At

Why the selected rows become unselected if the list is scrolled (See the pictures)? Xcode 12.2. iOS 14.2.

I also get a console message:

[Assert] Attempted to call -cellForRowAtIndexPath: on the table view while it was in the process of updating its visible cells, which is not allowed.

Update

This seems to be iOS 14.2 bug. I downloaded a simulator for iOS 14.1 version and everything works just fine.

import SwiftUI

struct ContentView: View {

    @State private var selectedRows = Set<String>()
    var items = ["item1", "item2", "item3", "item4","item5","item6","item7","item8","item9","item10","item11","item12","item13","item14","item15","item16","item17","item18","item19","item20","item21","item22"]
    
    var body: some View {
        
        NavigationView {
            List(selection: $selectedRows) {
                ForEach(items, id: \.self) { item in
                    Text(item)
                }
            }
            .listStyle(InsetGroupedListStyle())
            .environment(\.editMode, Binding.constant(.active))
            
        }
    }
}

Select cells Scroll the list up Scroll the list down

0

There are 0 answers