Search code examples
iosswiftlistviewswiftuireload

SwiftUI: List data dissapears after you dismiss sheet


I have a list of horizontal images. And a little profile/settings button in the navigation bar. When you click the profile button, a sheet view slides up, and when you dismiss it, the list seems to lose it's cells (HStacks of other images basically)

Here's the code that displays the sheet and list

    var body: some View {
        NavigationView {
            List {
                ForEach(0..<self.categories.count) {
                    ExploreRow(title: self.categories[$0])
                }
            }
            .navigationBarTitle("Justview")
            .navigationBarItems(trailing: profileButton)
            .sheet(isPresented: self.$showingProfile) {
                Text("User Profile: \(self.userData.userProfile.username)")
            }
        }
    }

ExploreRow is essentially a horizontal stack in a scroll view that displays images loaded from the web.

Before Sheet image:

enter image description here

After Sheet dismiss image:

enter image description here


Solution

  • I assume there are not many categories in the List (as they should not be many), so the simplest is just force-rebuild list (supposing you don't make anything heavy in ExploreRow.init, like

    List {
        ForEach(0..<self.categories.count) {
            ExploreRow(title: self.categories[$0])
        }.id(UUID())
    }
    .navigationBarTitle("Justview")
    

    *The issue is due to List's caching-reusing row views, it is known.