0

Ok, I'm pretty sure this is a bug on Apple's part.

  1. I fetch data using @SectionedFetchRequest.

  2. I want to show links to detail in grids divided by sections (a bit like iOS Photos, where individual photos show as rectangles separated by a certain period, e.g. November 2021.

  3. However, NavigationLink always opens the last item in a given grid, even if I tap some other item in this grid.

  4. The important part: this issue does not occur when using ScrollView. It only occurs when using List.

A minimal example is below. Any fixes/workarounds will be much appreciated!

import SwiftUI

struct ItemsListView: View {
    
    @EnvironmentObject var viewRouter: ViewRouter

    @SectionedFetchRequest(
      sectionIdentifier: SortModel.default.section,
      sortDescriptors: SortModel.default.descriptors,
      animation: .default
    )
    private var coreData: SectionedFetchResults<String, Item>
    
    
    let layout = [GridItem(.adaptive(minimum: 100)),
                  GridItem(.adaptive(minimum: 100)),
                  GridItem(.adaptive(minimum: 100))]
    
    var body: some View {
        
        List { // **Bug disappears when List replaced with Scrollview**

            ForEach(coreData) { section in
              Section(header: Text(section.id)) {
                  LazyVGrid(columns: layout, spacing: 10) {
                      ForEach(section) { item in
                          Button(action: {
                              viewRouter.selectedItem = item
                              viewRouter.detailActive = true
                          }, label: {
                              LabelView(item: item)
                          })
                      } //: foreach
                  } //: LazyVGrid
              } //: section
            } //: ForEach section
        } //: list
       
           VStack(spacing: 0) {
                NavigationLink(destination: ItemDetailView(item: viewRouter.selectedItem), isActive: $viewRouter.detailActive){ EmptyView() }
            }
            .hidden()

    } //: body
} //: struct



struct LabelView: View {
    let item: Item
    var body: some View {
                
             Image("\(item.image)") // some image
               .resizable()
               .scaledToFill()
               .clipped()
               .cornerRadius(12)

    } //: body
} //: struct

struct ItemDetailView: View {
    let item: Item
    var body: some View {

         VStack {
             Text("This is the detail view for \(item.name)"
         } //: vstack 

    } //: body
} //: struct

blu-Fox
  • 401
  • 6
  • 14

0 Answers0