ios - FetchedResultsController 和自定义 header 部分的数据

标签 ios uitableview swift2

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

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

section name 键是与父表的关系及其在父表中的属性名称之一。

我通过覆盖下面的内容来自定义节标题 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

在此 header 中,我想访问父实体及其一些其他属性(不仅仅是 sectionNameKeyPath 中提到的属性)

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

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

谢谢

最佳答案

我在声明 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
}

关于ios - FetchedResultsController 和自定义 header 部分的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35362152/

相关文章:

ios - 作为请求处理程序的结果更新 UI

jquery - 适用于 iOS 的 Cordova 应用程序不显示通过 ajax 加载的图像

ios - OCMock 方法名称冲突

ios - UITableViewController 不会调用 cellForRowAtIndexPath 但 numberOfSectionsInTableView 和 numberOfRowsInSection 设置正确

ios7 - -[__NSCFStringboundingRectWithSize :options:attributes:context:]: unrecognized selector sent to instance

ios - 在 TableView 中填充数组字典的高效且干净的方法

ios - 检测 UITableView 部分何时滚动出 View

ios - 无法成功编码 UIImage 并存储在数据库中

ios - 在 Swift 中的浏览器中打开 Web url 链接

arrays - swift 2.1 : How do you produce a set from an array?