I am new in SwiftUI, I have small project, it is work just for one list item, but it is not work for list items, like when I click the any list items, all details pass name of "david", I want to pass data of user which I clicked, where I missed here?
struct ImageModel: Identifiable, Hashable {
var id = UUID().uuidString
var name: String
}
var datas = [
ImageModel(name: "davis"),
ImageModel(name: "carry"),
ImageModel(name: "maria"),
]
struct ImageRowView: View {
var data: ImageModel
var body: some View {
NavigationLink(destination: ImageDetailsView(data: ImageModel)){
HStack{}
}}}
struct ImageDetailsView: View {
var body: some View {
ImageDetails(data: ImageModel(name: "davis"))
}
}
struct ImageDetailsView_Previews: PreviewProvider {
static var previews: some View {
ImageDetailsView()
}
}
struct ImageDetails : View {
var data: ImageModel
var body: some View{
VStack{
Text(data.name)
}
}
struct ImageHomeView: View {
var body: some View {
VStack(alignment: .leading, spacing: 15, content: {
ForEach(datas) {data in
ImageRowView(data: data)
}}}
CodePudding user response:
somewhere you have to loop through your array of items.
For example like this.
struct ImageModel: Identifiable, Hashable {
var id = UUID().uuidString
var name: String
}
struct ImageRowView: View {
var datas = [
ImageModel(name: "davis"),
ImageModel(name: "carry"),
ImageModel(name: "maria")
]
var body: some View {
NavigationView { // you need a NavigationView otherwise NavigationLink won't work
List { // you might want a List (as you mentioned it)
ForEach(datas) { item in // you need ForEach to loop through all items
NavigationLink(destination: ImageDetails(data: item)) {
Text(item.name) // here you show your list item
}
}
}
}
}
}
struct ImageDetails : View {
var data: ImageModel
var body: some View{
VStack {
Text("Detail View")
Text(data.name)
}
}
}
CodePudding user response:
Here you implement your Model, that looks good to me.
struct Image: Identifiable, Hashable {
var id = UUID().uuidString
var name: String
}
If you want to use the list view like you're doing in the question the image data is specified and static. So you can initialize them at directly in the struct. The goal is to parse the gives data into our ListView and InformationView. So we create a NavigationView to control the navigation. Inside of this NavigationView, we define a List which takes all items and loop through it (for-each).
struct ImageListView: View {
let images: [Image] = [
Image(name: "davis"),
Image(name: "carry"),
Image(name: "maria"),
]
var body: some View {
NavigationView {
List(images) { image in
NavigationLink(destination: {
InformationView(image: image)
.navigationTitle(image.name)
}, label: {
Text(image.name)
})
}
.navigationBarTitle("Images")
}
}
}
struct ImageListView_Previews: PreviewProvider {
static var previews: some View {
ImageListView()
}
}
Now you can use this view to controll the content which is displayed if you click one of the items. You can add properties to the Image model and automatically can access that inside the view because we use a property which stores a Image (e.g a description or a kicker).
struct InformationView: View {
let image: Image
var body: some View {
Text(image.name)
}
}