【发布时间】:2015-12-31 16:43:24
【问题描述】:
我的数据模型很简单,就像一本普通的字典:
字母是字母表中的字母,单词是字典中的单词和定义。我的目标是在 UITableView 中显示数据,其中字母是部分,下面是单词。
我的代码:
var fetchedResultsController: NSFetchedResultsController!
override func viewDidLoad() {
super.viewDidLoad()
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Letter")
let letterSort =
NSSortDescriptor(key: "letterName", ascending: true)
fetchRequest.sortDescriptors = [letterSort]
fetchedResultsController =
NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: context,
sectionNameKeyPath: "letterName",
cacheName: "dict")
fetchedResultsController.delegate = self
do {
try fetchedResultsController.performFetch()
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
func numberOfSectionsInTableView
(tableView: UITableView) -> Int {
return fetchedResultsController.sections!.count
}
func tableView(tableView: UITableView,
titleForHeaderInSection section: Int) -> String? {
let sectionInfo =
fetchedResultsController.sections![section]
return sectionInfo.name
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
let sectionInfo =
fetchedResultsController.sections![section]
print(sectionInfo.numberOfObjects)
return sectionInfo.numberOfObjects
}
我从教程中学到了这段代码。它似乎工作。节数等于 26,每个标题是 a 到 z。
问题是如何获取一个部分的行数,以及如何引用数据以将它们排成一行?
【问题讨论】:
标签: swift uitableview core-data ios9