GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.

Click to save your free spot now

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

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.