How to dynamically add a section for List

I am having a heck of a problem trying to figure out how to add Sections to a List in SwiftUI (dynamically based on a change in an array value). Everything works until I try and test a variable and add a section around the row. As soon as I add some logic I get the error: Unable to infer complex closure return type; add explicit type to disambiguate. I tried moving the section to a new function returning a View but that didn’t help either. Any thoughts on how to rewrite this: The SiteRow() is defined as a struct of some View and returns some stacks. I couldnt find any examples on how to do a normal list row and add a section dynamically, they were all hardcoded lists.

 

struct FetchView: View {

    @ObservedObject var fetcher = NOCFetcher()

    var lastCompany: String = “”

  

    var body: some View {

        NavigationView {

            VStack {

                List(fetcher.siteinfo) {     // The Unable to infer complex closure error is shown here at the curly bracket

                    site in

                    if site.customer != self.lastCompany {

                        Section(header: Text(site.customer)) {

                            SiteRow(site: site)

                        }

                        self.lastCompany = site.customer

                    } else {

                        SiteRow(site: site)

                    }

                }.navigationBarTitle(Text(“Site Listing”))

            }

        }

    }

}

Powered by WPeMatico

You may also like...

Comments are closed.