我试图使用NSFetchedResultController从核心数据中获取对象,但它不会在tableview中显示任何数据

时间:2017-09-05 16:25:27

标签: ios uitableview core-data

我正在尝试使用TableViewController类中的NSFetchedResultsController来获取对象。但这不会获取任何对象,也不会填充我的tableview。

以下是我正在使用的代码:

import UIKit
import CoreData

class TVCProductList: UITableViewController, NSFetchedResultsControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        JSProManagnedObjectContext = ((UIApplication.shared.delegate as! AppDelegate).persistentContainer).viewContext

        do {
            try JSProFetchedResultsController?.performFetch()
        } catch {
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }



    // MARK: - Table view data source =============================================================================================

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (JSProFetchedResultsController?.fetchedObjects?.count)!
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProductName", for: indexPath)
        let listProduct = JSProFetchedResultsController?.object(at: indexPath)
        cell.textLabel?.text = listProduct?.productName
        return cell
    }

    //=============================================================================================================================



    //MARK: - Setup NSFetchResultsController ==========================================================================================

    var JSProManagnedObjectContext : NSManagedObjectContext?
    var JSProFetchedResultsController : NSFetchedResultsController<JSProducts>? = nil


    var AJSProFetchedResultsController : NSFetchedResultsController<JSProducts> {
        if JSProFetchedResultsController != nil {
            return JSProFetchedResultsController!
        }
        let JSProFetchRequest : NSFetchRequest<JSProducts> = JSProducts.fetchRequest()
        let JSProSortDescriptor = NSSortDescriptor(key: "productName", ascending: true)
        JSProFetchRequest.sortDescriptors = [JSProSortDescriptor]

        let BJSProFetchedResultsController = NSFetchedResultsController(fetchRequest: JSProFetchRequest, managedObjectContext: self.JSProManagnedObjectContext!, sectionNameKeyPath: "productName", cacheName: nil)
        BJSProFetchedResultsController.delegate = self

        JSProFetchedResultsController = BJSProFetchedResultsController
        do {
            try JSProFetchedResultsController?.performFetch()
        } catch {
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }
        return JSProFetchedResultsController!
    }

    //================================================================================================================================

}

1 个答案:

答案 0 :(得分:0)

看起来你已经将它设置为显示0个部分,而且,这就是它正在做的事情。 :-p将其更改为1。