SwiftUI Select Multiple items in a list at once (Select ALL)

Viewed 137

I have created a List using Foreach

ForEach (Array(options.enumerated()),id:\.offset) { index , option in
                        ListItem(optionName: option)
                      
                    }
                }

and Implemented the following logic for selection

struct ListItem {
    var optionList : Options
    @Binding var selections : Set<UUID>
    var isSelected : Bool{
        selectedItem.contains(optionList.id)
    }
    var body : some  View {
        HStack{
            //View for list row
            
            
            if isSelected  {
                Image(systemName: "checkmark")
                    .foregroundColor(Color.primaryAlertAccentColor)
            }
        }
        .onTapGesture {
            if isSelected {
                self.selections.remove(option.id)
            }
            else {
                self.selections.insert(option.id)
            }
            
        }
    }
    
}

A single List Item is also created using ListItem.

When I Tap on that all the remaining options should be selected. For this I need to append multiple items to Set. How can I do that ? or any alternative solution?

Edit : Insert can be used to append only one item I want to selct all the available Items in that Also tried the solution using sets and didnt work, since i created a custom type

None of the above answers solve the qustion. Looks like someone is in a hurry to close the questions which he dont have any answer

0 Answers
Related