2016-04-06 4 views
-3

Ich möchte Daten anzeigen, die bereits in den Kerndaten gespeichert wurden, und ich möchte nur eine Vorschau anzeigen, genauso wie die Daten des Buchnamens gespeichert und in der Tabellenansicht angezeigt werden. Alle Tutorials, die ich gesehen habe, sind bis jetzt, nehmen normalerweise Textfelder, geben die Daten ein, speichern sie und holen sie dann. Bitte helfen Sie mir, weil dies meine erste Interaktion mit KerndatenWie zeigen Sie die Daten an, die bereits in den Stammdaten gespeichert wurden?

+0

Sie müssen ein bisschen mehr Forschung als das tun. Die Tutorials, die Sie erwähnen, zeigen Ihnen, wie Sie Daten einfügen, so dass Sie das ohne Textfelder tun können. – Mundi

Antwort

0

Von dem, was ich verstehe Sie versuchen, den NSfetchedResults Controller zu verwenden. Wenn Ihre Daten un ein Array es zu Kerndaten speichern seiner Sie folgendermaßen vorgehen, wenn Sie swift verwenden ...

// erstellen Sie Ihre Array

var employee:NSMutableArray = [] 
employee.addObject(["name":"Bill","LastName":"Hanks"]) 
employee.addObject(["name":"Rolex","LastName":"Swarzer"]) 
employee.addObject(["name":"Clive","LastName":"Martin"]) 
employee.addObject(["name":"Jimi","LastName":"Hendrix"]) 

Das fügen Sie Daten Kern wie folgt :

let appDel = UIApplication.sharedApplication().delegate as! AppDelegate 
     let context = appDel.managedObjectContext 

     for item in employee { 
      do { 
       let newUser = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext: context) 
       newUser.setValue(item["name"], forKey: "name") 
       newUser.setValue(item["LastName"], forKey: "lastname") 
       try context.save() 
      } catch { 
       //do nothing 
      } 
     } 

Sie wissen Unter der Annahme, wie die UITableView einzurichten und haben ihre Eigenschaft TableBox beachten Sie die folgenden Methoden genannt. Sie können kopieren und einfügen und nur ändern, was notwendig ist

// --------------------------------- ---- Last tableViewMethods -----

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return "" 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    let sectionInfo = self.fetchedResultsController.sections![section] 
    return sectionInfo.numberOfObjects 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return (self.fetchedResultsController.sections?.count)! } 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("theCell", forIndexPath: indexPath) 

    let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject 
    self.configureCell(cell, withObject: object) 

    return cell 
} 

func configureCell(cell: UITableViewCell, withObject object: NSManagedObject) { 
    cell.textLabel!.text = object.valueForKey("name")!.description 
    cell.detailTextLabel!.text = object.valueForKey("lastname")!.description 
} 

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return false if you do not want the specified item to be editable. 
    return true 
} 

func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    return true 
} 

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { 
    tableBox.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath) 
} 

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     let context = self.fetchedResultsController.managedObjectContext 
     context.deleteObject(self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject) 

     do { 
      try context.save() 
     } catch { 
      // Replace this implementation with code to handle the error appropriately. 
      // abort() causes the application to generate a crash log and terminate. 
      abort() 
     } 
    } 
} 

// -------- geholt Ergebnisse Controller Denken Sie daran, den Entity Name und die NSSortdescriptor Schlüssel zu ändern. Ändern Sie auch die Eigenschaft "tableBox" in Ihren Namen mit dem Namen

var fetchedResultsController: NSFetchedResultsController { 
    if _fetchedResultsController != nil { 
     return _fetchedResultsController! 
    } 
    let appDel = UIApplication.sharedApplication().delegate as! AppDelegate 
    let context = appDel.managedObjectContext 
    let fetchRequest = NSFetchRequest(entityName: "Employee") 

    // Set the batch size to a suitable number. 
    //fetchRequest.fetchBatchSize = 20 

    // Edit the sort key as appropriate. 
    let sortDescriptor = NSSortDescriptor(key: "name", ascending: true) 

    fetchRequest.sortDescriptors = [sortDescriptor] 

    // Edit the section name key path and cache name if appropriate. 
    // nil for section name key path means "no sections". 
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil) 
    aFetchedResultsController.delegate = self 
    _fetchedResultsController = aFetchedResultsController 

    do { 
     try _fetchedResultsController!.performFetch() 
    } catch { 
     // Replace this implementation with code to handle the error appropriately. 
     // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
     //print("Unresolved error \(error), \(error.userInfo)") 
     abort() 
    } 

    return _fetchedResultsController! 
} 
var _fetchedResultsController: NSFetchedResultsController? = nil 

func controllerWillChangeContent(controller: NSFetchedResultsController) { 
    self.tableBox.beginUpdates() 
} 

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) { 
    switch type { 
    case .Insert: 
     self.tableBox.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade) 
    case .Delete: 
     self.tableBox.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade) 
    default: 
     return 
    } 
} 

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
    switch type { 
    case .Insert: 
     tableBox.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade) 
    case .Delete: 
     tableBox.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) 
    case .Update: 
     self.configureCell(tableBox.cellForRowAtIndexPath(indexPath!)!, withObject: anObject as! NSManagedObject) 
    case .Move: 
     tableBox.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!) 
    } 
} 

func controllerDidChangeContent(controller: NSFetchedResultsController) { 
    self.tableBox.endUpdates() 
}