Iphone 当使用NSFetchedResultsController和sectionNameKeyPath是一种关系时,如何获取节名?
在以下代码中:Iphone 当使用NSFetchedResultsController和sectionNameKeyPath是一种关系时,如何获取节名?,iphone,nsfetchedresultscontroller,sectionheader,Iphone,Nsfetchedresultscontroller,Sectionheader,在以下代码中: NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:myManagedObjectContext sectionNameKeyPath:@"store" cacheName:@"SomeCache" ]; sectionNameKeyPath的@“store”值实际上指向一
NSFetchedResultsController *frc =
[[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:myManagedObjectContext
sectionNameKeyPath:@"store"
cacheName:@"SomeCache"
];
sectionNameKeyPath的@“store”值实际上指向一个与store实体的关系,该实体有一个我真正需要的name属性作为我的节头标题
但是,我的代码设置方式无法实现,因为我得到的部分标题如下:
0x5b504f0
0x5b51190
据我所知,这是正在获取的存储对象的代码数据地址
如何使用NSFetchedResultsController,以便告诉它我希望它从sectionNameKeyPath:@“store”中获取任何内容的name属性?或者有其他解决方法吗?试试
sectionNameKeyPath:@“store.name”
我知道这是一个旧线程,但我想使用Swift添加我的解决方案
我必须指出,此解决方案取决于NSFetchedResultsController
返回的节名格式,因此它取决于苹果可以更改的内部实现
节名包含持久数据存储中对象ID的URI,因此可以通过首先提取URI,然后向存储请求具有相应URI的对象来获取对象
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let sectionInfo = fetchedResultsController?.sections![section]
let sectionName = sectionInfo?.name
//This part depends on the internal implementation of sectionInfo.name
let initialRange = sectionName?.rangeOfString("<")
let finalRange = sectionName?.rangeOfString(">")
let uri = sectionName?.substringWithRange((initialRange?.endIndex)!..<(finalRange?.startIndex)!)
let url = NSURL(string: uri!)
let store = fetchedResultsController?.managedObjectContext.persistentStoreCoordinator
let objectID = store?.managedObjectIDForURIRepresentation(url!)
let coreObject = fetchedResultsController?.managedObjectContext.objectWithID(objectID!)
//return the correct property from the object as the title
let title = ...
return title
}
override func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String?{
让sectionInfo=fetchedResultsController?.sections![section]
让sectionName=sectionInfo?.name
//此部分取决于sectionInfo.name的内部实现
让initialRange=sectionName?.rangeOfString(“”)
让uri=sectionName?.substringWithRange((initialRange?.endIndex)!…谢谢!以下内容也有助于提供道具: