我已经被困在这几天了,似乎无法弄明白。 我正在使用带有CoreDate的NSFetchedResultsController,并希望在更新核心数据对象时更新行的UI。在我的情况下,当用户完成练习时,状态的值更改为1.有趣的是,我可以打印出正确的值,但无法更新UI。谢谢!
class CoreDataTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var fetchedResultsController: NSFetchedResultsController? {
didSet {
do {
if let frc = fetchedResultsController {
frc.delegate = self
try frc.performFetch()
}
tableView.reloadData()
} catch let error {
print("NSFetchResultsController.performFetch() failed: \(error)")
}
}
}
// Mark: UITableViewDataSource (Part 1)
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return fetchedResultsController?.sections?.count ?? 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedResultsController?.sections where sections.count > 0 {
return sections[section].numberOfObjects
} else {
return 0
}
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let sections = fetchedResultsController?.sections where sections.count > 0 {
return sections[section].name
} else {
return nil
}
}
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return fetchedResultsController?.sectionIndexTitles
}
override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
return fetchedResultsController?.sectionForSectionIndexTitle(title, atIndex: index) ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ExerciseCell", forIndexPath: indexPath)
configureCell(cell, atIndexPath: indexPath)
return cell
}
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let exerciseObject = fetchedResultsController!.objectAtIndexPath(indexPath) as! CoreExerciseForPlan
let cell = tableView.dequeueReusableCellWithIdentifier("ExerciseCell", forIndexPath: indexPath)
cell.detailTextLabel?.text = exerciseObject.exercise?.name
cell.textLabel?.text = String(exerciseObject.status)
}
// Mark: NSFetchedResultsControllerDelegate (Part 2)
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Insert: tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Update: tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete: tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default: break
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
let exerciseObject = fetchedResultsController!.objectAtIndexPath(indexPath!) as! CoreExerciseForPlan
switch type {
case .Update:
print("indexPath: \(indexPath!.row)")
print("object: \(exerciseObject.exercise!.name) with status \(exerciseObject.status)")
**// These print the correct values for status!!!!**
configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Move:
tableView.deleteRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
}