0

I have a pretty simple view:

import SwiftUI


struct DataSceneSectionView: View {


    @ObservedObject var model: DataSceneSectionModel


    var body: some View {

        ForEach(Array(model.entries.enumerated()), id: \.element) { (index, entry) in

            NavigationLink(destination: DataSceneEntryView(model: entry.model)) {

                Text(entry.title)
                .tag(index)
            }
        }
    }
}

where the model looks like this:

import Foundation
import SwiftUI


final class DataSceneSectionModel: ObservableObject {


    @Published var entries: [DataSceneSectionEntry] = []


    var dataTypeGroup: DataTypeGroup


    init(dataTypeGroup: DataTypeGroup) {

        self.dataTypeGroup = dataTypeGroup

        self.entries = buildEntries()
    }


    private func buildEntries() -> [DataSceneSectionEntry] {

        return DataType.allCases
            .filter({

                return $0.group == dataTypeGroup
            })
            .map({

                return DataSceneSectionEntry(dataType: $0)
            })
    }
}


struct DataSceneSectionEntry: Identifiable, Hashable {

    var id = UUID()

    var dataType: DataType

    var title: String { return dataType.localizedPlural }
    var model: DataSceneEntryModel { return DataSceneEntryModel(dataType: dataType) }
}

Now, there is a strange effect: When tapping one of the entries, the linked view is shown correctly. After returning and tapping a different entry, the linked view for that one is shown correctly as well. BUT: When tapping the same entry as before, it is just highlighted, but no action is performed.

I suspected the missing tag modifier, but this didn't change anything...

Any idea how this can be fixed?

Hardy
  • 4,344
  • 3
  • 17
  • 27
  • Does this answer your question? [NavigationLink Works Only for Once](https://stackoverflow.com/questions/59279176/navigationlink-works-only-for-once) – Asperi Feb 10 '20 at 17:38
  • Thanks for the hint. I tried it, but have very strange effects (tapping on one entry opens as much times the subview as elements in the ForEach exist). Beside this, I do not want to change the default navigation, like in the suggestion solutions. – Hardy Feb 10 '20 at 19:05
  • I tried compiling your code by adding missing views and models, but it's a lot of work. If you post an example that compiles and demonstrates the issue, that would greatly help getting answers. – Bart van Kuik Feb 11 '20 at 07:33
  • I created a gist here with the full code: https://gist.github.com/innoreq/c37b429f239aa54b254d4e6f00013b8f – Hardy Feb 11 '20 at 15:45
  • Interestingly, I get this console message when tapping the second time on a menu entry: >>>2020-02-11 16:59:01.770623+0100 XYZ[32788:2101443] [TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). – Hardy Feb 11 '20 at 16:01
  • This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window. – Hardy Feb 11 '20 at 16:01
  • Table view: <_TtC7SwiftUIP33_BFB370BA5F1BADDC9D83021565761A4925UpdateCoalescingTableView: 0x7f887a040600; baseClass = UITableView; frame = (0 0; 1024 1322); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = ; layer = ; contentOffset: {0, -103.5}; contentSize: {1024, 352.5}; adjustedContentInset: {103.5, 0, 50, 0}; dataSource: <_TtGC7SwiftUIP13$7fff2c69da4419ListCoreCoordinatorGVS_20SystemListDataSourceOs5Never_GOS_19SelectionManagerBoxS2___: 0x7f889b20ed20>> – Hardy Feb 11 '20 at 16:01

0 Answers0