FetchedResultsController and data for custom header section

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


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
}
链接地址: http://www.djcxy.com/p/36184.html

上一篇: Unix shell脚本找出脚本文件所在的目录?

下一篇: FetchedResultsController和自定义标题部分的数据