Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i remove any image from an array by clicking ? i try it with .remove (at:0) but it didn't work it removes only in order Using Swift

enter image description here

how can i remove any image from an array by clicking ? i try it with .remove (at:0) but it didn’t work it removes only in order and my goal is to remove any of them

 ForEach(mediaItems.items,id: \.id) {    item in
                                ZStack{
                                HStack {
                                    if item.mediaType == .photo {
                                    Image(uiImage:item.photo ?? UIImage())
                                        .resizable()
                                        .frame(width: 112, height: 90)
                                        .cornerRadius(13)
                                    } else if item.mediaType == .video {
                                        if let url = item.url {
                                            VideoPlayer(player: AVPlayer(url: url))
                                                .frame(minHeight: 200)
                                        } else { EmptyView() }
                                    }
                                    
                                }
                                     
                                    
                                    
                                    Image("Tracé 2424")
                                        .offset(x: 40, y: -30)
                                        .onTapGesture{
                                    //        mediaItems.items.remove(at:0)
                                        }

                                        
                                }
                            } //foreach

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use .enumerated() to get an index for each item. You’ll need to change:

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

to:

ForEach(Array(mediaItems.items.enumerated()), id: \.1.id) { index, item in

and then use index in mediaItems.items.remove(at: index):

ForEach(Array(mediaItems.items.enumerated()), id: \.1.id) { index, item in
    ZStack {
        HStack {
            if item.mediaType == .photo {
                Image(uiImage:item.photo ?? UIImage())
                    .resizable()
                    .frame(width: 112, height: 90)
                    .cornerRadius(13)
            } else if item.mediaType == .video {
                if let url = item.url {
                    VideoPlayer(player: AVPlayer(url: url))
                        .frame(minHeight: 200)
                } else { EmptyView() }
            }
                                
        }
                                                                   
        Image("Tracé 2424")
            .offset(x: 40, y: -30)
            .onTapGesture {
                mediaItems.items.remove(at: index)
        }
                                    
    }
} //foreach
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading