FetchedResultsController和自定义标题部分的数据

我正在使用带有sectionNameKeyPath的fetchedResultsController,如下所示。

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

部分名称键是父表与其父表中的一个属性名的关系。

通过重写下面的func tableView(tableView:UITableView,viewForHeaderInSection section:Int) - > UIView?

在这个头文件中,我想访问父实体及其几个其他属性(不仅仅是sectionNameKeyPath中提到的属性)

我没有对属性为“APropertyName”的父实体强制执行任何唯一性。

我想在编写该部分的自定义标题时查询父实体。 我如何实现这一目标?

谢谢


我使用了与父类和子类的一对多关系,并在声明fetchedResultsController时使用“objectID”作为sectionNameKeyPath。

下面是fetchedResultsController的减速。

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

一旦提取完成并准备在单元格上显示标题信息,我使用fetchedResultsController.sections?[section] .objects属性来遍历父级。 以下是渲染自定义标题单元格的代码。

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/36183.html

上一篇: FetchedResultsController and data for custom header section

下一篇: UIManagedDocument and NSFetchedResultsController