-1

(Sample code is added)

I am trying to use SwiftUI to build a selectable list like the original Tableview in Swift.

I would like to show a detail view when the user tab on the selected item. However, the only way I find to implement it is to use NavigationView and NavigationLink.

Do anyone know is there any other way to implement a selectable list by SwiftUI?

Below is the sample I implemented :

NavigationView{

    List{

        ForEach(items, id: \.id){ item in 

            NavigationLink(destination: WorkItemDetailView(item: item)){

                WorkItemListRow(item: item)
            }
        }
    }
}

WorkItemListRow is to present the object in the cell of the list. WorkItemDetailView is the detail view will show the detail of the object.

  • And why can't you use NavigationView and NavigationLink? – LuLuGaGa Oct 30 '19 at 07:56
  • @LuLuGaGa Sorry for not making my question clear. I can use NavigationView and NavigationLink but I would like to know is there any other way to implement a list view. – Liling Chen Oct 30 '19 at 08:37
  • If you just don’t like the layout with the navigation bar, maybe this is helpful: https://stackoverflow.com/questions/57517803/how-to-remove-the-default-navigation-bar-space-in-swiftui-navigiationview – cbjeukendrup Oct 30 '19 at 21:34
  • @cbjeukendrup Thank you. Although this is not what I am looking for, it's still helpful for me. – Liling Chen Oct 31 '19 at 04:29

1 Answers1

2

You could also use a modal presentation:

@State var selectedItem: Item? = nil

var body: some View {
    List {
        ForEach(items) { item in
            WorkItemListRow(item: item)
                .onTapGesture {
                    self.selectedItem = item
                }
        }
    }   .sheet(item: $selectedItem,
               onDismiss: { self.selectedItem = nil }) { item in
                    WorkItemDetailView(item: item)
               }
    }
}
LuLuGaGa
  • 13,089
  • 6
  • 49
  • 57