CoreData - TableView 没有得到更新
Posted
技术标签:
【中文标题】CoreData - TableView 没有得到更新【英文标题】:CoreData - TableView does not get updated 【发布时间】:2015-07-29 02:13:55 【问题描述】:所以,我正在努力学习如何正确处理 CoreData。
这是我的代码:
import UIKit
import CoreData
class IngredientsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var fetchedResultsController: NSFetchedResultsController?
override func viewDidLoad()
super.viewDidLoad()
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchIngredients(), managedObjectContext: moc!, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController?.delegate = self
fetchedResultsController?.performFetch(nil)
func fetchIngredients() -> NSFetchRequest
var fetchRequest = NSFetchRequest(entityName: "DetailsForRecipe")
let sortDescriptor = NSSortDescriptor(key: "ingredients", ascending: true)
fetchRequest.predicate = nil
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.fetchBatchSize = 20
return fetchRequest
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return fetchedResultsController?.sections?[section].numberOfObjects ?? 0
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("ingCell", forIndexPath: indexPath) as! UITableViewCell
if let ingCell = fetchedResultsController?.objectAtIndexPath(indexPath) as? DetailsForRecipe
cell.textLabel?.text = ingCell.ingredients
return cell
和
import UIKit
import CoreData
class SingleIngredientViewController: UIViewController
@IBOutlet var ingField: UITextField!
var moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
@IBAction func addIng(sender: AnyObject)
let entityDescription = NSEntityDescription.entityForName("DetailsForRecipe", inManagedObjectContext: moc!)
let details = DetailsForRecipe(entity: entityDescription!, insertIntoManagedObjectContext: moc)
details.ingredients = ingField.text
var error: NSError?
moc?.save(&error)
if let err = error
var status = err.localizedFailureReason
println(status)
else
println("Ingredient \(ingField.text) saved successfully!")
if let navigation = navigationController
navigation.popViewControllerAnimated(true)
我的模特:
import Foundation
import CoreData
class DetailsForRecipe: NSManagedObject
@NSManaged var name: String
@NSManaged var ingredients: String
@NSManaged var image: NSData
应用程序应在文本字段中插入成分名称,将其保存到 coreData,然后应在表格视图中检索它。当我给成分名称发短信并按“添加”时,prinln 消息说它已成功保存,但表格视图没有更新。
我在这里做错了什么?我不是开发人员,我已经阅读了很多关于如何做到这一点的教程,但这非常令人困惑!所以请原谅我。 提前致谢!
【问题讨论】:
【参考方案1】:我还没有阅读您的完整源代码,但是通过立即浏览,我没有看到您调用了 tableview 的 reloadData
函数。如果您没有为您的表格视图设置 IBOutlet,您将需要这样做,然后在其上调用 reloadData()
。
【讨论】:
【参考方案2】:进行更改后,在您的表格视图上调用 reloadData()
。
【讨论】:
我应该在哪里调用 reloadData()?在 viewDidLoad 它抛出一个错误 我不确定这两个视图控制器是如何相关的,但SingleIngredientViewController
必须以一种或另一种方式调用IngredientsViewController
上的方法,然后再调用self.tableView.reloadData()
。您可以使用名为IngredientsViewControllerDelegate
的协议和名为didAddDetail
的方法创建委托关系,然后让IngredientsViewController
实现它并由SingleIngredientViewController
调用它。以上是关于CoreData - TableView 没有得到更新的主要内容,如果未能解决你的问题,请参考以下文章
无法将 tableView.deleteRowsAtIndexPaths 与 coreData 一起使用