WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

How to Customize Section with SectionedFetchResults

Forums > SwiftUI

I am making a list view of tasks that are organized by groups and these groups also have a color assinged to them. My goal is to have the section header have the name of the group with the group color as the text color.

@SectionedFetchRequest<String?, Task>(
        sectionIdentifier: \.group,
        sortDescriptors: [NSSortDescriptor(keyPath: \Task.group, ascending: true),
                          NSSortDescriptor(keyPath: \Task.title, ascending: true)]
    ) private var sortedByGroup: SectionedFetchResults<String?, Task>
var body: some View {
        NavigationView {
            Form {
                List {
                    ForEach(sortedByGroup) { selectedGroup in
                        Section(header: Text(selectedGroup.id ?? "error")) {
                            ForEach(selectedGroup) { task in
                                NavigationLink {
                                    TaskDetailView(task: task)
                                } label : {
                                    ...
                                }
                            }
                        }
                    }
                }
            }
        }
    }

As you can see I have only been able to get the text to work so far

2      

Hi. As I have no idea how you structured your Task Entity in Core Data and how you have you color assigned to group. I will base my suggestion on assumptions :)

As a possible solution you can try to add extension to TaskEntity for computed property like so:

var viewGroupColor: Color {
  if group1 {
  return .gray
  } else if group2 {
    return .green
  }

  // etc.
  // or you can create enum and switch between groups to assign a color
}

Then use initalizer for Section which accepts header as a closure and assign color respectively via foregroundColor modifier:

 Section {
                  ForEach(selectedGroup) { task in
                      NavigationLink {
                          TaskDetailView(task: task)
                      } label : {
                          ...
                      }
                  }
              } header: {
                Text(selectedGroup.id ?? "error")
                  .foregroundColor(selectedGroup.viewGroupColor) // not sure how you have set it up
                  // but logic as you have your group chosen in TaskEntity it will assign color accordingly in computed property.
              }

2      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.