2015-07-27 16 views
6

Quando sono nel mezzo di una ricerca e quindi passare UItabs, ViewWillDisappear non viene chiamato. Qualche idea sul motivo per cui ViewWillDisappear non viene chiamato quando ho filtrato i risultati visualizzati e passare le schede?ViewWillDisappear non viene chiamato searchcontroller

func updateSearchResultsForSearchController(searchController: UISearchController) { 
    if self.searchController?.searchBar.text.lengthOfBytesUsingEncoding(NSUTF32StringEncoding) > 0 { 
     if let results = self.results { 
      results.removeAllObjects() 
     } else { 
      results = NSMutableArray(capacity: MyVariables.dictionary.keys.array.count) 
     } 

     let searchBarText = self.searchController!.searchBar.text 

     let predicate = NSPredicate(block: { (city: AnyObject!, b: [NSObject : AnyObject]!) -> Bool in 
      var range: NSRange = NSMakeRange(0, 0) 
      if city is NSString { 

       range = city.rangeOfString(searchBarText, options: NSStringCompareOptions.CaseInsensitiveSearch) 
      } 

      return range.location != NSNotFound 
     }) 

     // Get results from predicate and add them to the appropriate array. 
     let filteredArray = (MyVariables.dictionary.keys.array as NSArray).filteredArrayUsingPredicate(predicate) 
     self.results?.addObjectsFromArray(filteredArray) 


     // Reload a table with results. 
     self.searchResultsController?.tableView.reloadData() 
    } 
} 




override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(self.identifier) as! UITableViewCell 

    var text: String? 
    var imgtext:AnyObject? 
    if tableView == self.searchResultsController?.tableView { 
     if let results = self.results { 
      text = self.results!.objectAtIndex(indexPath.row) as? String 
      imgtext = MyVariables.dictionary[text!] 
      let decodedData = NSData(base64EncodedString: imgtext! as! String, options: NSDataBase64DecodingOptions(rawValue: 0)) 
      var decodedimage = UIImage(data: decodedData!) 


      cell.imageView?.image = decodedimage 
     } 
    } else { 
     text = MyVariables.dictionary.keys.array[indexPath.row] as String 
    } 

    cell.textLabel!.text = text 


    return cell 
} 

sul carico

override func viewDidLoad() { 
    super.viewDidLoad() 

    let resultsTableView = UITableView(frame: self.tableView.frame) 
    self.searchResultsController = UITableViewController() 
    self.searchResultsController?.tableView = resultsTableView 
    self.searchResultsController?.tableView.dataSource = self 
    self.searchResultsController?.tableView.delegate = self 

    // Register cell class for the identifier. 
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier) 
    self.searchResultsController?.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier) 

    self.searchController = UISearchController(searchResultsController: self.searchResultsController!) 
    self.searchController?.searchResultsUpdater = self 
    self.searchController?.delegate = self 
    self.searchController?.searchBar.sizeToFit() 
    self.searchController?.hidesNavigationBarDuringPresentation = false; 
    self.tableView.tableHeaderView = self.searchController?.searchBar 
    self.definesPresentationContext = true 

} 
+0

Quale visualizzazione del controller di visualizzazioneDisplay è visibile? Il controller di visualizzazione tabella principale o searchResultsController? – pbasdf

+0

@pbasdf UITableViewController –

+1

@ user5130344 .. Ho anche affrontato lo stesso problema? Hai trovato qualche soluzione? – Saty

risposta

-3

penso che il suo problema con l'Xcode. Prova a chiuderlo e riapri il progetto ancora una volta e prova a eseguire di nuovo

+1

Non è xcode, l'ho ricompilato più volte e chiuso xcode e riaperto decine di volte ... C'è qualcosa nel mio codice che non permette il rilascio della pagina e non riesco a capire di cosa si tratta –

+0

prova a commentare il codice che ritieni un po 'sospetto (es: codice relativo alla ricerca) e poi prova a navigare tra le schede e vedere il problema persiste perché non riesco a trovare il problema –

7

Aveva lo stesso problema. viewWillDisappear non viene chiamato su UITableViewController, ma viene chiamato nello UISearchController.

Quindi ho creato una sottoclasse di UISearchController e ho annullato il metodo viewWillDisappear. Nel mio caso ho solo bisogno di disattivare il controller di ricerca.

class SearchController: UISearchController { 

    override func viewWillDisappear(_ animated: Bool) {   
     // to avoid black screen when switching tabs while searching 
     isActive = false 
    } 
} 
Problemi correlati