TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SectionedFetchedRequest object selection

Forums > SwiftUI

With this code I save a selected Student from a list generated from FetchRequest:

struct StudentsView: View {

@FetchRequest var studentsFetchRequest: FetchedResults<Student>
@State private var selectedStudent: Student?

var body: some View {

List(studentsFetchRequest, id: \.self, selection: $selectedStudent) { student in
     NavigationLink(destination: StudentDetailView(student: student)) {
        StudentCellView(student: student)
      }
    } 
 //...

But then, how can I save selected Activity from a list generated from this SectionedFetchRequest?

struct ActivitiesView: View {

@SectionedFetchRequest(sectionIdentifier: \.dateAsMonth!, sortDescriptors: [SortDescriptor(\.date, order: .reverse)]) private var activities: SectionedFetchResults<String, Activity>
@State private var selectedActivity: Activity?

var body: some View {

List (selection: $selectedActivity) {  // <---
    ForEach(activities) { section in
       Section(header: Text(section.id)) {
          ForEach(section) { activity in
             NavigationLink(destination: ActivityDetailView(activity: activity)) {
                ActivityCellView(activity: activity)
              }
            }
         //...

This way I can check that selectedActivity returns "nil". What am I doing wrog?

2      

For more information, I've tried this sintax as well, still not accesing any selectedActivity at all:

List (activities, selection: $selectedActivity){ section in
     Section(header: Text(section.id)) {
       ForEach(section) { activity in
         NavigationLink(destination: ActivityDetailView(activity: activity)) {
           ActivityCellView(activity: activity)
           }

And activities.count this doesn't return the number of "Activity" objects, but the number of Sections instead.

Any further orientation will be very useful. Thanks!

2      

Seems like List with selection init does not work with sectioned lists...

May be as an option you can attach onTapGesture to ActivityCellView(activity: activity) and then assign selectedActivity = activity

2      

Thanks, @ygeras. But it works only "more or less".

My intention with this is to save selectedActivity.id as a String (uuidString) on a @AppStorage in order to, coming back again to that view, keep showed selection with this code snipets:


@AppStorage("selectedActivityId") var selectedActivityId = ""

//...

.onChange(of: selectedActivity, perform: { selection in
      selectedActivityId = selection?.id?.uuidString ?? ""
          })

//...

.onAppear{
    if activities.first != nil {
        selectedActivity = Array(activities).first { $0.id?.uuidString == selectedActivityId }
            }
        }

It works well on some other (not-sectioned Fetchrequest, just simple ones) views of my app.

Your proposal "catches" selectedActivity, but only when clicking (it's a MacOS app) on the part of the cell which does have text. But this way the cell itself is not "grafically" selected. And still, I'll have the problem of selecting again back this activity .onAppear, as there is no way to be "inserted" on list view hierarchy 😅

2      

I've found the origin of the problem with .count returning items on Apple forums:

https://developer.apple.com/forums/thread/689453

But still no way to deal with selected items on SectionedFetchRequests 🥴

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.