我知道这是一个旧线程,但我想使用 Swift 添加我的解决方案。
我必须建议这个解决方案取决于返回的部分名称的格式NSFetchedResultsController
,因此它取决于 Apple 可以更改的内部实现。
部分名称包含持久数据存储中对象 ID 的 URI,因此您可以通过先提取 URI,然后向 Store 请求具有相应 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
}
好吧,你需要检查错误(我会guard
在前面使用很多let
),但你明白了。