1

I am using the fetchedResultsController with the sectionNameKeyPath as below.

let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: "relName.APropertyName", cacheName: nil)

the section name key is the relationship to the parent table and its one of the property name in the parent table.

I have a custom section header by overriding the below func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

in this header i would like to access the parent entity and its few other properties ( not just the property mentioned in the sectionNameKeyPath)

I have not enforced any uniqueness on the parent entity with the property "APropertyName" .

I would like to query the parent entity when i write the custom header for the section. How do I achieve this?

Thanks

1 Answer 1

2

I used the one to many relationship with the parent and child and used the "objectID" as the sectionNameKeyPath while declaring the fetchedResultsController.

Below was the deceleration of fetchedResultsController.

    let fetchRequest = NSFetchRequest(entityName: "Child")
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: "relParent.objectID", cacheName: nil)

Once the fetch is complete and ready to display the header information on the cell I used fetchedResultsController.sections?[section].objects property to traverse to the parent. below is the code to render the custom header cell.

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let  headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as? ChildEntityHeaderCell
    if let cell = headerCell {

        if  let sectionData = fetchedResultsController.sections?[section] {
            if sectionData.objects != nil && sectionData.objects!.count > 0 {
               if  let child = sectionData.objects?[0] as? ChildEntity , parent = child.relChild // child entity has inverse relationship with the parent [ two way relationship] 
               {
                if let name = parent.PropertyA {
                  cell.LabelField.text = name

                }


                }
            }

        }

    }
    return headerCell
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.