1

Using the multiple selection from a list in SwiftUI ioS 14.7.1, 14.6 and 14.5 does not visually retain the selection when scrolled off screen. The selections themselves are retained properly in the variable. A GIF to demonstrate:

Selection visual not retained

This happens both on the device and the sim. It seems like a bug but perhaps I am missing something?

struct RegionDetailView: View {
    
    @EnvironmentObject var model:BrainViewModel
    
    @State private var multiSelection = Set<Int>()
    @State private var showSaveScreen = false
    @State private var saveName = ""
    
    var body: some View {
        List(selection: $multiSelection) {
            Section(header: Text("Default Segments")) {
                ForEach(model.brainSegments.sorted(by: {$0.fullName < $1.fullName}),id:\.id) { segment in
                    Text("\(segment.fullName) (\(segment.shortName))")
                }
            }
            Section(header: Text("Custom Segments")) {
                ForEach(model.customBrainSegments,id:\.id) { segment in
                    Text("\(segment.fullName) (\(segment.shortName))")
                }
            }
        }
        .environment(\.editMode, .constant(.active))
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Button {
                    showSaveScreen.toggle()
                } label: {
                    Text("Save")
                }
                .popover(isPresented: $showSaveScreen) {
                    
                }
            }
        }
        .onAppear {
            model.loadCustomSegments()
        }
    }
C6Silver
  • 3,127
  • 2
  • 21
  • 49
  • Does this answer your question? [How does one enable selections in SwiftUI's List](https://stackoverflow.com/questions/56706188/how-does-one-enable-selections-in-swiftuis-list) – lorem ipsum Aug 12 '21 at 23:31
  • No. My issue is not in creating a multiselect list view, it's with the fact that the selected items are visually disappearing when scrolled off screen even though the variable properly contains everything selected. – C6Silver Aug 12 '21 at 23:59
  • Can you show a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). This is intriguing, but we can't run your code as you have custom types in it. – Yrb Aug 13 '21 at 15:08

1 Answers1

0

I have the same issue with SwiftUI List on iOS 14. It seems that it is fixed on iOS15 but it would be nice to have some workaround for the older version.

Here is minimal reproducible example:

struct ContentView: View {
    @State var selected: Set<Int> = []


@State private var items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

var body: some View {
    VStack {
        EditButton()
        List(selection: $selected) {
            ForEach(items.indices) { index in
                let item = items[index]
                Text("Item \(item)")
                    .tag(item)
                    .onTapGesture {
                        if selected.contains(item) {
                            selected.remove(item)
                        } else {
                            selected.insert(item)
                        }
                    }
            }
            .onDelete(perform: {
                items.remove(atOffsets: $0)
            })
        }
    }
    .onChange(of: selected) { newValue in
        print("selected: \(newValue)")
    }
}

}

To reproduce it, select some items at the top, then scroll to the bottom and back to the top.