教程_CoreData实例(二)_构建数据层
Swift教程_CoreData实例(三)_构建控制层(列表数据加载、删除数据)
四、构建控制层
控制层总体结构包括列表的数据加载、数据的新增、删除、更新。这里我们先来搞定列表controller的功能(数据加载、删除),即PKOBooksTableViewController。
1.列表数据加载、删除数据
我们自定义一个列表控制器PKOBooksTableViewController,并应用到storyboard的列表中。通过NSFetchedResultsController对象来查询、删除数据。
代码如下,注释非常详尽:
- import UIKit
- import CoreData
-
- class PKOBooksTableViewController: UITableViewController,NSFetchedResultsControllerDelegate {
-
- var managedObjectContext: NSManagedObjectContext?
-
- var fetchedResultsController: NSFetchedResultsController?
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
-
- self.navigationItem.leftBarButtonItem = self.editButtonItem()
-
-
- var error: NSError? = nil
- if !self.initFetchedResultsController().performFetch(&error){
- NSLog("Unresolved error \(error), \(error!.userInfo)")
- abort()
- }
- }
-
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
-
-
- func setCellInfo(cell: UITableViewCell, indexPath: NSIndexPath) {
- var book = self.fetchedResultsController?.objectAtIndexPath(indexPath) as Book
- NSLog("======\(book.title)")
- cell.textLabel.text = book.title
- }
-
-
-
- override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
-
- return self.fetchedResultsController!.sections!.count
- }
-
- override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-
- var section = self.fetchedResultsController!.sections![section] as NSFetchedResultsSectionInfo
- return section.numberOfObjects
- }
-
-
- override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
-
- self.setCellInfo(cell, indexPath: indexPath)
- return cell
- }
-
- override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
-
- return self.fetchedResultsController!.sections![section].name
- }
-
-
-
-
-
-
-
-
-
-
- override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
- if editingStyle == .Delete {
-
- var context = self.fetchedResultsController?.managedObjectContext
- context!.deleteObject(self.fetchedResultsController?.objectAtIndexPath(indexPath) as NSManagedObject)
-
- var error: NSError? = nil
- if context?.save(&error) == nil {
- NSLog("Unresolved error \(error), \(error!.userInfo)")
- abort()
- }
- } else if editingStyle == .Insert {
- }
- }
-
-
-
-
-
-
-
-
-
- override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
-
- return false
- }
-
-
-
-
- func initFetchedResultsController() ->NSFetchedResultsController
- {
- if self.fetchedResultsController != nil {
- return self.fetchedResultsController!
- }
-
- var fetchRequest = NSFetchRequest()
- var entity = NSEntityDescription.entityForName("Book", inManagedObjectContext: self.managedObjectContext!)
- fetchRequest.entity = entity
-
-
- var authorDescriptor = NSSortDescriptor(key: "author", ascending: true)
- var titleDescriptor = NSSortDescriptor(key: "title", ascending: true)
- var sortDescriptors = [authorDescriptor, titleDescriptor]
- fetchRequest.sortDescriptors = sortDescriptors
-
-
- var fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "author", cacheName: "Root")
- fetchedResultsController.delegate = self
- self.fetchedResultsController = fetchedResultsController
- return fetchedResultsController
- }
-
-
- func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
- switch(type) {
- case .Insert:
- self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
- case .Delete:
- self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
- case .Update:
- self.setCellInfo(self.tableView.cellForRowAtIndexPath(indexPath!)!, indexPath: indexPath!)
- case .Move:
- self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
- self.tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
- }
- }
-
-
- func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
- switch(type) {
- case .Insert:
- self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic)
- case .Delete:
- self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic)
- case .Update:
- break
- case .Move:
- break
- }
- }
-
-
- func controllerWillChangeContent(controller: NSFetchedResultsController) {
-
- self.tableView.beginUpdates()
- }
-
-
- func controllerDidChangeContent(controller: NSFetchedResultsController) {
- self.tableView.endUpdates()
- }
-
-
-
-
-
-
-
-
-
-
- }
原文地址:http://blog.csdn.net/ooppookid/article/details/40867835