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()
        }
    }
3
  • Does this answer your question? How does one enable selections in SwiftUI's List Commented Aug 12, 2021 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. Commented Aug 12, 2021 at 23:59
  • Can you show a Minimal, Reproducible Example. This is intriguing, but we can't run your code as you have custom types in it. Commented Aug 13, 2021 at 15:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe you should try creating a separate View for row and maintain selection there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.