简体   繁体   中英

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:

在此处输入图片说明

After Sheet dismiss image:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM