0

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


@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?


@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 wrong?

For more information, I've tried this syntax 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.

Although there is a similar question here, it doesn't solve mine, as that is for multi selection (Set), but not mine. As well, the use of selectedActivity.ID option described there make the app not to compiling and throwing errors.

arroyot24
  • 39
  • 1
  • 4
  • Does this answer your question? [List with multiple selections from core data](https://stackoverflow.com/questions/76417534/list-with-multiple-selections-from-core-data) – lorem ipsum Jun 18 '23 at 19:26

0 Answers0