Swift:在使用NSFetchedResultsController应用谓词后重新加载tableview数据

时间:2016-01-25 18:10:00

标签: ios swift uitableview core-data

我使用NSFetchedResultsController来填充UITableView,我试图添加类别过滤器,所以如果用户选择一个需要在UITableView上重新加载数据的类别,这就是我和#39;我试图这样做。

var categoriaAtual : Int?

var fetchedResultsController: NSFetchedResultsController {

        if _fetchedResultsController != nil {
            print("Already fetch")
            return _fetchedResultsController!
        } else {
            print("New Fetch")
        }

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext
        self.managedObjectContext = managedContext

        let fetchRequest = NSFetchRequest()
        // Edit the entity name as appropriate.
        let entity = NSEntityDescription.entityForName("Frases", inManagedObjectContext: self.managedObjectContext!)
        fetchRequest.entity = entity


        // Set the batch size to a suitable number.
        fetchRequest.fetchBatchSize = 20

        // Edit the sort key as appropriate.
        let sortDescriptor = NSSortDescriptor(key: "favorita", ascending: false)



        fetchRequest.sortDescriptors = [sortDescriptor]
        fetchRequest.fetchLimit = 20

        if categoriaAtual != nil {
            print("Categoria Atual \(categoriaAtual)")
            fetchRequest.predicate = NSPredicate(format: "categoria = %d",categoriaAtual!)
            fetchRequest.fetchLimit = 2
        } else {
            print("No predicate");
        }

       //
        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
        let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
        aFetchedResultsController.delegate = self
        _fetchedResultsController = aFetchedResultsController

        do {
            try _fetchedResultsController!.performFetch()

        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            //print("Unresolved error \(error), \(error.userInfo)")
            abort()
        }

        return _fetchedResultsController!
    }

    var _fetchedResultsController: NSFetchedResultsController? = nil

func setarCategoria(cat: Int) {
        categoriaAtual = cat
        _fetchedResultsController = nil
        self.tableView.reloadData()
}

现在我有一个名为MenuTableView.swift的幻灯片菜单,我这样打电话:(我认为问题在这里)

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let homeVC = storyboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeTableViewController
    homeVC.setarCategoria(indexPath.row)

    self.slideMenuController()?.closeLeft()

}

我确实得到了打印说这是一个新的提取,但TableView根本没有改变。

1 个答案:

答案 0 :(得分:0)

instantiateViewControllerWithIdentifer创建一个viewController的新实例(因此是fetchedResultsController的一个新实例),这就是你如何获得" New Fetch"记录,但您的原始表格视图不会更改。您可能只需要为MenuTableView提供HomeViewController可以实现的委托。