I have a bunch of cells in a vertical scroll view, but am seeing some weird behavior from the onTapGesture
and fullScreenCover
for those cells. When I click on a cell, it registers the correct cell in onTapGesture
but presents the wrong object, in the fullScreenCover
it looks like it's being executed many times, and presenting the wrong object. Not sure why this is happening, anyone have any insight on what the issue could be?
Here are the print statements from clicking 1 cell:
TAP HERE: 1935
FULL SCREEN: 1942
FULL SCREEN: 1940
FULL SCREEN: 1935
FULL SCREEN: 1934
struct TakesList: View {
@ObservedObject var viewModel: TakesListViewModel
@State private var isPresented = false
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
LazyVStack {
ForEach(viewModel.takes, id: \.self) { take in
ListTakeCell(presenter: TakeCellViewModel(take: take))
.padding(.horizontal, 10)
.padding(.vertical, 6)
.onTapGesture {
let _ = print("TAP HERE: \(take.id)")
UIImpactFeedbackGenerator(style: .light).impactOccurred()
isPresented = true
}
.fullScreenCover(isPresented: $isPresented, onDismiss: nil, content: {
let _ = print("FULL SCREEN: \(take.id)")
SingleTakeView(take: take)
.edgesIgnoringSafeArea(.bottom)
})
}
}
}
}
}
Maybe you can try this:
Added the
selectedTake
property. This is kind of a workaround for not being able to usefullScreenCover(item
. When the cell is tapped, you set theselectedTake
property with thetake
that was tapped. Then adidSet
gets triggered which checks to make sureselectedTake
isn'tnil
. If it isn't, then it setsisPresented
totrue
. ThefullScreenCover(isPresented
modifier is moved out of the list onto theLazyVStack
which still gets triggered ifisPresented
becomestrue
. I haven't tested this, but it's an idea that could work.Curious if this works for you. Cheers!