【问题标题】:How to add additional informations in NSFetchedResultsController's section如何在 NSFetchedResultsController 的部分添加其他信息
【发布时间】:2016-01-14 06:52:06
【问题描述】:
我在UITableView 中有一个自定义部分,它有一个UIImageView 和一个UILabel。使用NSFetchedResultsController 我可以根据剖面模型设置图像。假设截面模型有imageName 和sectionTitle。基于当前的实现,我得到了<NSFetchedResultsSectionInfo> 作为一个部分对象。这是否可以通过部分对象中的自定义对象获得。
谢谢
【问题讨论】:
标签:
ios
uitableview
core-data
nsfetchedresultscontroller
nsfetchrequest
【解决方案1】:
我想我知道你的意思。您创建了一个 NSFetchedResultsController 对象,将某个实体作为 sectionNameKey 的参数传递。因此,您的自定义部分基于此实体。我猜这就是你所说的“截面模型”。
如果要访问section中的对象,可以使用以下代码:
//create array for demonstration purposes
var exampleArray = [[YourEntityType]]()
for sectionInfo in fetchedResultsController.sections! {
//this will give you an array with all the objects in the section
let objectsForSection = sectionInfo.objects as! [YourEntityType]
//this will add the array above to another array, so you will have access to all the objects of all the sections
exampleArray.append(objectsForSection)
}
你也可以使用keyValue编码,像这样:
//create sectionInfo variable, where 2 is the number of the section (the third section in this example
let sectionInfo = fetchedResultsController.sections![2]
//access the needed entity object from the sectionInfo
let exampleVariable = (sectionInfo.objects as! AnyObject).valueForKeyPath("yourEntity") as! YourEntityType
//access the needed attribute object from the sectionInfo
let anotherExampleVariable = (sectionInfo.objects as! AnyObject).valueForKeyPath("yourEntity.yourAttribute") as! YourAttributeType
【解决方案2】:
一旦您知道与该部分关联的对象,您就可以确定要显示的图像和标签文本。
确保您的对象(可能是与获取结果控制器获取的主要实体的关系)提供了必要的信息。