2015-08-25 158 views
0

我想過濾一個UITableView而不需要涉及到搜索欄。不使用搜索欄來過濾UITableView

func filterResultsBasedOnSideViewController() { 

     print("filter value \(filterString)") 

     filteredPredicate = NSPredicate(format: "keyWords contains[c] %@", filterString) 
     filteredObjects = self.fetchedResultsController.fetchedObjects?.filter() { 
      return self. filteredPredicate!.evaluateWithObject($0) 
      } as! [myValues]? 

     self.tableView.reloadData() 


    } 

但是在調用這個時真的沒有什麼事情發生。我在這裏完全錯了嗎?

而且我fetchedResultsController是誰的不知道的人如下:這裏

var fetchedResultsController: NSFetchedResultsController { 
     if _fetchedResultsController != nil { 
      return _fetchedResultsController! 
     } 

     let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
     self.managedObjectContext = appDel.managedObjectContext 

     let fetchRequest = NSFetchRequest() 
     // Edit the entity name as appropriate. 
     let entity = NSEntityDescription.entityForName("myValues", 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: "sortID", ascending: true) 

     fetchRequest.sortDescriptors = [sortDescriptor] 

     // 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: "Master") 
     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 controllerWillChangeContent(controller: NSFetchedResultsController) { 
     self.tableView.beginUpdates() 
    } 
+0

嘗試_fetchedResultsController =無調用filterResultsBasedOnSideViewController – AntonijoDev

+0

@AntonijoDev謝謝,試過,但無法正常工作。 ( –

+0

)你是否在fetchedResultsController方法中使用了puttin斷點?當你進入它時會發生什麼? – AntonijoDev

回答

0

確定是工作的代碼。感謝大家的意見,指出我的方式正確。

我創建了一個var filterIsOn = false 在我NSFRC我把一個if語句:

if filterIsOn { 

     //  print("is on") 

      let filteredText: NSString = sprite 
      filteredPredicate = NSPredicate(format: "keyWords contains[c] %@", filteredText) 

      let sortDescriptor = NSSortDescriptor(key: "sortID", ascending: true) 
      fetchRequest.predicate = filteredPredicate 
      fetchRequest.sortDescriptors = [sortDescriptor] 

     } 

,並改變了:

let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil) //set cacheName to nil from "Master" 

刪除func filterResultsBasedOnSideViewController() { }並設置filterIsOn = true當我需要篩選結果。

簡單! ;)