Taking the example from SO answer itself where multiple selection in a list is performed. Next step is how to show detail view of selected items together in detail view. Not able to figure out this issue. Created a button which will redirect to detail view based on selected items but it showing only one item
My app involves API therefore I am taking this example. In addition, the detail view of list item in my app comes from another API instead of the API used to create list. I think I have to use published var to store selected items (how?) and than call API
import SwiftUI
struct HomeActivity: View {
@State var items: [String] = ["Apples", "Oranges", "Bananas",
"Pears",
"Mangos","grapefruit","Kela","Angur","Papita","tarbooj"]
@State var selection: [String] = []
@State var listId: [String] = []
@State var yes:Bool = false
var body: some View {
NavigationView{
VStack{
Button("Submit"){
self.yes.toggle()
}
if(yes)
{
//implement detail view based on selection
}
}
List{
ForEach(self.items, id: \.self){item in
// NavigationLink(destination: Text("Fruit Name : \(item)")){
DetailView(title:item,isSelected:
self.selection.contains(item)) {
if self.selection.contains(item) {
self.selection.removeAll(where: { $0 == item })
// self.yes.toggle()
}
else {
listId = self.selection
}
}
}
}
}
}
}
struct DetailView: View {
var title: String
var isSelected: Bool
var action: () -> Void
var body: some View {
Button(action: self.action) {
HStack {
Text(self.title)
if self.isSelected {
Spacer()
Image(systemName: "checkmark")
}
}
}
}
}
struct HomeActivity_Previews: PreviewProvider {
static var previews: some View {
HomeActivity()
}
}