简体   繁体   中英

Swift: Reload tableview data after applying a predicate with NSFetchedResultsController

I'm using the NSFetchedResultsController, to populate a UITableView, i'm trying to add a category filter, so if the user choose a category a need to reload the data on the UITableView, this is what i'm trying to do.

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()
}

Now i have a slide out menu called MenuTableView.swift,i make the call like this: ( I Think the problem is here )

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()

}

I do get the print saying that this is a new fetch, but the TableView does not change at all.

instantiateViewControllerWithIdentifer creates a new instance of the viewController (and therefore a new instance of the fetchedResultsController), which is how you get the "New Fetch" log but your original table view doesn't change. You probably just need to give MenuTableView a delegate that your HomeViewController can implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM