SwiftUI - ForEach in List shows the wrong row in detail with iOS 15 but the right one in iOS 14

In a List where I have a ForEach works fine for iOS 14 real device and in the simulator when I tap a row that shows detail but not with my iPhone X with iOS 15. Other iPhone X with iOS 14 is showing the right detail from the selection.

macOS Big Sur Xcode 12.5.1

Here is the code:

@ObservedObject var teamStore = TeamStore() @State private var showDetail = false @State private var viewState = CGSize.zero

NavigationView {

            List {

                ForEach(teamStore.teams, id: .id) { team in

                    Section(header: Text(getLocalizedTeam(team))) {

                        ForEach(team.ringer) { wrestler in

                            Button(action: {

                                showDetail.toggle()

                            }, label: {

                                WrestlerRow(wrestler: wrestler)

                            })

                            .sheet(isPresented: $showDetail, content: {

                                WrestlerDetailView(wrestler: wrestler)

                            })

                        } //: FOREACH

                    } //: SECTION

                } //: FOREACH

            } //: LIST

...     When I build and run on my iPhone X with Xcode Version 13.0 beta 5 (13A5212g) the same issue and the text foreground color is in blue.

Thanks for suggestions

Regards Berkant

Did you try:

        ForEach(teamStore.teams, id: \.self) { team in

Yes, I did and the problem is the same. I tried it with the iOS 15 simulator and the same issue and the foreground color from the text views changed to blue.

What's the id for team.ringer (wrestler)? I've had similar problems (incorrect List/ForEach item displayed) recently in iOS15 when I'd inadvertently used an id that wasn't unique. I've also had the blue text issue. I'm using Xcode 13 beta.

you have potentially many "sheet(..)" triggered by just one variable "showDetail". I'm amazed you got anything to work. Re-structure your code to trigger only the relevant sheet, when you press the button. For example, using ".sheet(item:...)"

Hi,

Thank you, the problem is probably with the .sheet (). If I use the NavigationLink instead of the button, it works as intended. Now I just have to use the .sheet () to make it work that way

I have similar issue, illustrated with this cut-down code and an alert in place of a sheet which always shows the first item, irrespective of the user selection. Its the same in iOS14 and 15.

	let items = ["One", "Two", "Three"]
	@State private var showAlert: Bool = false

    var body: some View {
		List {
			ForEach(items, id: \.self) { item in
				Text(item)
					.onTapGesture() {
						showAlert = true
					}
					.alert(isPresented: $showAlert, content: {
						Alert(title: Text(item), message: Text(item), dismissButton: .default(Text("OK")))
					})
			}
		}
    }

A workaround is to add an additional State variable to hold the selected item, like this:

let items = ["One", "Two", "Three"]
	@State private var showAlert: Bool = false
	@State private var selectedItem: String = ""

    var body: some View {
		List {
			ForEach(items, id: \.self) { item in
				Text(item)
					.onTapGesture() {
						selectedItem = item
						showAlert = true
					}
					.alert(isPresented: $showAlert, content: {
						Alert(title: Text(selectedItem), message: Text(selectedItem), dismissButton: .default(Text("OK")))
					})
			}
		}
    }
SwiftUI - ForEach in List shows the wrong row in detail with iOS 15 but the right one in iOS 14
 
 
Q