0

I am trying to render a list of entities that are split into sections (think CoreData NSFetchedResultsSectionInfo).

I believe this solution (or very similar) used to be in the documentation. I have tried several ways of doing this, either I get the scrolling issue or pinnedViews won't pin.

I also asked this question in Apple Dev Forum, no takers so far.

This my solution, however it renders poorly. The ScrollView's content is far too long, scrolling is not fluid and may freeze.

    struct SectionsGridView: View {
        let sections: Sections<ListItemViewModel>
        let columns = [ GridItem(.adaptive(minimum: .cellSize)) ]
    
        var body: some View {
            ScrollView {
                LazyVGrid(columns: columns, spacing: .gridSpacing, pinnedViews: .sectionHeaders) {
                    ForEach(sections) { list in
                        Section(header: Text(list.title?.uppercased() ?? "error")) {
                            ForEach(list) { item in
                                GridCell().environmentObject(item)
                            }
                        }
                    }
                }
            }
        }
    }

    private extension CGFloat {
        static let gridSpacing = 8.0
        static let cellSize = 100.0
    }
Jeremy Quinn
  • 51
  • 1
  • 3
  • 1
    Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and see: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example (MRE)](https://stackoverflow.com/help/minimal-reproducible-example). You haven't given enough code, as this won't build, and I am not sure exactly what you are asking. – Yrb Jul 07 '23 at 14:05
  • Please provide a minimal compilable code to reproduce. Generally I would say 1 - .environmentObject(item)` - there's probably no reason for that (it's just a propery most likely, or pssibly a binding, if it's changing); 2 - `ForEach(section)` looks weird. Did you mean to loop over some property of the section?; 3 - `let results: Sections` - not sure how you are filling these values, but since it's not a @State and inside a view, it may be reinitializing on every view redraw. – timbre timbre Jul 07 '23 at 17:25
  • Thanks for your response, responding to your points: 1. Common pattern in Apple samples, passing the ViewModel in the environment. 2. Sections is a wrapper for `NSFetchedResultsSectionInfo` and implements `RandomAccessCollection` wrapping each entity in the declared ViewModel. 3. Passed as a parameter as unable to put generics into Environment. I do not believe the issue is with the models as I use these patterns all over the App. The problems only start when trying to render the Sections in a Grid. I will make a PoC with static data to see if this is reproducible. Thanks! – Jeremy Quinn Jul 08 '23 at 09:22

1 Answers1

0

Resolved the problem. The layout above works fine, the problem was with my Model. Items in the collection had unstable identifiers.

Jeremy Quinn
  • 51
  • 1
  • 3