【发布时间】:2017-03-23 23:52:55
【问题描述】:
如何将数据存储在 CoreData 中?我正在使用它在我的 tableView 单元格中显示它,但是如何从我的数据库中保存和检索?
代码如下:
var arrOfDict = [[String :AnyObject]]()
var dictToSaveNotest = [String :AnyObject]()
class SecondViewController: UIViewController {
@IBAction func addItem(_ sender: Any)
{
dictToSaveNotest.updateValue(notesField.text! as AnyObject, forKey: "notesField")
arrOfDict.append(dictToSaveNotest)
}
然后在tableViewCell中查看,我使用了代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellID") as! CustomCell
cell.textView.text = arrOfDict[indexPath.row]["notesField"] as! String!
}
我在我的addItem 中试过这个
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let task = Task(context: context)
task.notes=notesField.text!
//save the data to coreData
(UIApplication.shared.delegate as! AppDelegate).saveContext()
但不确定。
【问题讨论】:
标签: ios swift uitableview core-data