【问题标题】:How to load my data back from CoreDate to display it in tableView?如何从 CoreDate 加载我的数据以在 tableView 中显示?
【发布时间】: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


    【解决方案1】:

    试试这个方法:

    let toDoItem = exercises[indexPath.row]
    
    cell.textLabel?.text =  toDoItem["exercises"]!
    

    更多参考请查看这个答案How to make a table from a dictionary with multiple content types in Swift?

    也许这会对你有所帮助。

    【讨论】:

    • 很高兴为您提供帮助..:)
    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多