【发布时间】:2015-02-19 11:01:40
【问题描述】:
我是使用 CoreData 的新手 - 所以我尝试设置一个应用程序,用户可以在其中通过按钮将一些文本保存到 CoreData 并将其加载回以在 tableView 中显示。我想出了如何保存数据,但我不知道如何正确加载它。它必须在存储新信息和加载视图时加载。
class ViewControllerExercises: UIViewController {
@IBOutlet weak var textField: UITextField!
@IBAction func tappedAddButton(sender: AnyObject) {
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var newExercises = NSEntityDescription.insertNewObjectForEntityForName("Exercises", inManagedObjectContext: context ) as NSManagedObject
newExercises.setValue(textField.text,forKey:"exercises")
context.save(nil)
}
func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return exercises.count
}
func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as UITableViewCell
var toDoItem:NSDictionary = exercises.objectAtIndex(indexPath!.row) as NSDictionary
cell.textLabel!.text = toDoItem.objectForKey("exercises") as? String
return cell
}
【问题讨论】:
标签: ios uitableview swift core-data save