如何在 NSFetchedResultsController 部分添加附加信息
How to add additional informations in NSFetchedResultsController's section
我在 UITableView
中有一个自定义部分,它有一个 UIImageView
和一个 UILabel
。使用 NSFetchedResultsController
我可以根据截面模型设置图像吗?假设截面模型有 imageName
和 sectionTitle
。基于当前的实现,我得到了 <NSFetchedResultsSectionInfo>
作为一个部分对象。是否可以通过部分对象中的自定义对象获取。
谢谢
我想我明白你的意思了。您创建了一个 NSFetchedResultsController 对象,将某个实体作为参数传递给 sectionNameKey。所以你的自定义部分是基于这个实体的。我猜这就是你说的 'section model' 的意思。
如果要访问节中的对象,可以使用以下代码:
//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
一旦您知道与该部分关联的对象,您就可以确定要显示的图像和标签文本。
确保您的对象(大概是与获取的结果控制器获取的主要实体的关系)提供了必要的信息。
我在 UITableView
中有一个自定义部分,它有一个 UIImageView
和一个 UILabel
。使用 NSFetchedResultsController
我可以根据截面模型设置图像吗?假设截面模型有 imageName
和 sectionTitle
。基于当前的实现,我得到了 <NSFetchedResultsSectionInfo>
作为一个部分对象。是否可以通过部分对象中的自定义对象获取。
谢谢
我想我明白你的意思了。您创建了一个 NSFetchedResultsController 对象,将某个实体作为参数传递给 sectionNameKey。所以你的自定义部分是基于这个实体的。我猜这就是你说的 'section model' 的意思。
如果要访问节中的对象,可以使用以下代码:
//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
一旦您知道与该部分关联的对象,您就可以确定要显示的图像和标签文本。
确保您的对象(大概是与获取的结果控制器获取的主要实体的关系)提供了必要的信息。