0

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()
    }
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
tintin
  • 335
  • 2
  • 8
  • 1
    I can't say I've understood your code and intention, would you elaborate more. – Asperi Aug 22 '22 at 08:17
  • So your detail view should display a list of items ? So it should be define with an array of items . – Ptit Xav Aug 22 '22 at 08:31
  • @Asperi need multiple selection in list....then detail of all selected items should be shown together in detail view....my app is pulling data for list from one API and data for detail view from another API with one id in common..this is why tried to present simple example – tintin Aug 22 '22 at 12:52
  • @PtitXav please read above comment – tintin Aug 22 '22 at 12:52

0 Answers0