ios - CoreData - TableView 未更新

标签 ios swift core-data save nsfetchrequest

所以,我正在努力学习如何正确处理 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 中,然后应在 TableView 中检索它。当我输入成分名称并按“添加”时,prinln 消息显示它已成功保存,但表格 View 没有更新。

我在这里做错了什么?我不是开发人员,我已经阅读了很多关于如何执行此操作的教程,但它非常令人困惑!所以请原谅我。 提前致谢!

最佳答案

我还没有阅读您的完整源代码,但是通过立即浏览,我没有看到您调用了 tableview 的 reloadData 函数。如果您没有为您的 TableView 设置 IBOutlet,您将需要这样做,然后对其调用 reloadData()

关于ios - CoreData - TableView 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31690141/

相关文章:

swift - 在 Swift 中为核心数据实现 NSValueTransformer 的位置

ios - 如何从 UIViewcontroller 的弹出窗口中的 UItableview 中填充数据?

ios - 在 Core Data 中存储时间间隔的正确方法是什么?

ios - 将 PNG 图像从 Firestore 设置为 UIImage.image --- Swift

ios - NSSortDescriptor 比 swift 高阶函数有什么优势?

ios - UISearchController 显示不正确的结果

ios - AVMutableComposition 的性能问题 - scaleTimeRange

ios - 如何在 Swift 中禁用 Collection View 中的特定单元格

ios - 从 Firebase Swift 下载时函数循环

iphone - 有没有办法为 Core Data 原语访问器自动生成 @property 和 @dynamic 标签?