I have a ForEach loop with a sheet opening when user taps on a row. The data on the sheet opened is based on which row did the user select. My code:
@State private var showMoodModal = false
@State var activeMoodForModal = 0
...
ForEach(moodEntries, id: \.self) { entry in
MoodTableViewCard(entry: entry).onTapGesture {
self.activeMoodForModal = moodEntries.lastIndex(of: entry) ?? 0
self.showMoodModal.toggle()
}
}.sheet(isPresented: $showMoodModal) {
MoodEntryModalView(entry: moodEntries[self.activeMoodForModal], isShown: self.$showMoodModal)
}
The problem is no matter which row user selects on the first tap, it creates a MoodEntryModalView with entry: moodEntries[0]
. If you dismiss the sheet and try to tap on another row it works fine. But if you relaunch the app the first try will always create the MoodEntryModalView with entry: moodEntries[0]
no matter which row did you tap. How do I fix this?