I can’t see the images leaving some even if I name them the same even if I run it or use simulator it’s the same.

iam seeing some of the images but not in every row or column any answers. And any ideas on how to print text below every image like a description, I tried in one of them writing denim but it didn't work. Like this:-

  • For your next question, please try pasting your code into a code block (using "Paste and Match Style"), instead of using screenshots.

  • 👍

Add a Comment

Accepted Reply

In a SwiftUI ForEach, each item must be uniquely Identifiable.
In your ForEach, you use the "id" parameter, to specify how to uniquely identify each item...

ForEach(clothimages, id: \.self)

If your items are not unique (they are not!), then they will not display correctly.
This is how ForEach works.

In your clothimages, it is the image name that must be unique, for the ForEach to work.
Update clothimages, so that all image names are unique.


To print text below your images, try:

func maincustom(content: Image, text: String) -> some View {
    VStack {
        content
            .resizable()
            .frame(width: 200, height: 250)
        Text(text)
    }
    .padding(8)
}
  • So it means if Iam trying to print same images I need to remove id else use different images, am I right?

  • You cannot remove the id. Use different images. Read the documentation on ForEach.

  • Get it running with unique images, then ask a new question about how to repeat images.

Replies

In a SwiftUI ForEach, each item must be uniquely Identifiable.
In your ForEach, you use the "id" parameter, to specify how to uniquely identify each item...

ForEach(clothimages, id: \.self)

If your items are not unique (they are not!), then they will not display correctly.
This is how ForEach works.

In your clothimages, it is the image name that must be unique, for the ForEach to work.
Update clothimages, so that all image names are unique.


To print text below your images, try:

func maincustom(content: Image, text: String) -> some View {
    VStack {
        content
            .resizable()
            .frame(width: 200, height: 250)
        Text(text)
    }
    .padding(8)
}
  • So it means if Iam trying to print same images I need to remove id else use different images, am I right?

  • You cannot remove the id. Use different images. Read the documentation on ForEach.

  • Get it running with unique images, then ask a new question about how to repeat images.

So it means if I am trying to print same images I need to remove id else use different images, am I right?

Not sure to understand.

What do you mean by "print same image" ?

To change what is on screen, you have to update the dataSource, clothimages

  • What I understood is that I need to use id for unique images and not to use id for the same images in the array. Is it? Correct me if Iam wrong.

Add a Comment

What I understood is that I need to use id for unique images and not to use id for the same images in the array. Is it? Correct me if Iam wrong.

No, you can have the same image content, they are different objects in the array. So you can use id. And image will appear several times.