我想在 tableview 上自动更新..为此我使用了 NSFetchedResultsControlleras

lazy var fetchedResultsController: NSFetchedResultsController = { 
    // Initialize Fetch Request 
    let fetchRequest = NSFetchRequest(entityName: "Student") 
 
    // Add Sort Descriptors 
    let sortDescriptor = NSSortDescriptor(key: "grade", ascending: true) 
    fetchRequest.sortDescriptors = [sortDescriptor] 
 
    // Initialize Fetched Results Controller 
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) 
 
    // Configure Fetched Results Controller 
    fetchedResultsController.delegate = self 
 
    return fetchedResultsController 
}() 

但我必须在用户选择加载后过滤结果......

  @IBAction func onSegmentValueChanged(sender: UISegmentedControl) 
    { 
        if sender.selectedSegmentIndex == 0{ 
 
            //filter by grade 
            let fetchRequest = NSFetchRequest(entityName: "Student") 
            let sortDescriptor = NSSortDescriptor(key: "grade", ascending: true) 
            fetchRequest.sortDescriptors = [sortDescriptor] 
            let filterfetchResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) 
            let predicate = NSPredicate(format: "address = %@","Russia") 
            filterfetchResultsController.fetchRequest.predicate = predicate 
            self.tableView.reloadData() 
 
        }else{ 
 
            let fetchRequest = NSFetchRequest(entityName: "Student") 
            let sortDescriptor = NSSortDescriptor(key: "grade", ascending: true) 
            fetchRequest.sortDescriptors = [sortDescriptor] 
            let filterfetchResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) 
            let predicate = NSPredicate(format: "grade = %d",10) 
            filterfetchResultsController.fetchRequest.predicate = predicate 
            self.tableView.reloadData() 
 
 
        } 
 
    } 

然而,这重新加载了 tableView,但数据未被过滤。我该如何解决这个问题? This是我正在处理的演示项目。还是应该使用正常方式传递结果并像正常方式一样重新加载 tableView 来解决此问题?

请您参考如下方法:

  1. 您必须使用 self.fetchedResultsController,这是您的数据源,而不是 filterfetchResultsController:没有连接。
  2. 您必须执行 performFetch() 来执行更改的谓词

这对我有用:

@IBAction func onSegmentValueChanged(sender: UISegmentedControl) 
{ 
    if sender.selectedSegmentIndex == 1{ 
 
        //filter by address 
        let predicate = NSPredicate(format: "address == %@","Russia") 
        self.fetchedResultsController.fetchRequest.predicate = predicate 
 
    }else{ 
 
        let predicate = NSPredicate(format: "grade = %d",10) 
        self.fetchedResultsController.fetchRequest.predicate = predicate 
 
    } 
 
    do { 
        try self.fetchedResultsController.performFetch() 
    } catch { 
        let fetchError = error as NSError 
        print("\(fetchError), \(fetchError.userInfo)") 
    } 
    self.tableView.reloadData() 
 
} 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!