Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Navigation link doesn't work when filtering ForEach

I have a segmented picker to filter the list rows inside a ForEach but the NavigationLinks stop working when filtering, they have the tap animation but never change view. I am using CoreData for the deck. When I tap the rows in Standard they work normally, then I tap Wild and the NavigationLinks still work but then when I go back to standard this is what happens:

enter image description here

This is the code I have:

 ForEach(decks.filter{$0.format == formats[selectedFormat]}, id: \.self) { deck in
                    HStack {
                        ZStack {
                            DeckRow(deck: deck)
                            NavigationLink(destination: DeckView()) {
                                EmptyView()
                            }
                        }
                    }
                }
like image 536
Happygallo Avatar asked May 21 '20 16:05

Happygallo


1 Answers

The provided code is not testable, but by reading assume the reason of issue is in equal navigation links, try the following (that will result in updating links on filtering)

ZStack {
    DeckRow(deck: deck)
    NavigationLink(destination: DeckView()) {
        EmptyView()
    }.id(deck)        // << here !!
}

or, as it would be expected, make that DeckView dependent on deck, like DeckView(model: deck), which also make each navigation link unique and refreshable on filter.

like image 186
Asperi Avatar answered Sep 20 '22 02:09

Asperi