2016-05-03 3 views
1

segueで遷移するセルをクリックした後、viewDidLoadでviewTableを参照しようとするとエラーが発生します。どうもありがとう!!は、テーブルから離れようとしている間にnilが見つかりました。ビュー

基本的に私はviewviewの参照をコメントアウトしない限りsegueを使うことはできません...しかし、私は検索バーを使うためにそれらを必要としています。 。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 


     @IBOutlet var tableView: UITableView! { 
      didSet { 
       print("tableView is set") 
      } 
     } 
     let searchController = UISearchController(searchResultsController: nil) 
     let textCellIdentifier = "TextCell" 
     var buildings: [(String,String)] = [] 
     var filteredBuildings = [(String,String)]() 
     var goToIndex: Int? 

     override func viewDidLoad() { 



      super.viewDidLoad() 

      print(tableView) 
      var buildingTuples = loadBuildings() 

      for tuple in buildingTuples { 
       self.buildings.append(tuple) 
      } 


      self.goToIndex = -1 

      searchController.searchResultsUpdater = self 
      searchController.dimsBackgroundDuringPresentation = false 
      definesPresentationContext = true 
      tableView!.tableHeaderView = searchController.searchBar 

     } 


     func filterContentForSearchText(searchText: String, scope: String = "All") { 
      filteredBuildings = buildings.filter { building in 
       return building.0.lowercaseString.containsString(searchText.lowercaseString) 
      } 

      self.tableView.reloadData() 
     } 

     // MARK: UITextFieldDelegate Methods 
     func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
      return 1 
     } 

     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      //return self.buildings.count 
      if searchController.active && searchController.searchBar.text != "" { 
       return filteredBuildings.count 
      } 
      return buildings.count 
     } 

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      /* 
      let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) 

      let row = indexPath.row 
      cell.textLabel?.text = buildings[row].0 

      return cell 
      */ 
      let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) 
      let tuple: (String, String) 
      if searchController.active && searchController.searchBar.text != "" { 
       tuple = filteredBuildings[indexPath.row] 
      } else { 
       tuple = buildings[indexPath.row] 
      } 
      cell.textLabel?.text = tuple.0 

      return cell 
     } 

     // MARK: UITableViewDelegate Methods 
     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      //tableView.deselectRowAtIndexPath(indexPath, animated: true) 

      let row = indexPath.row 
      self.goToIndex = indexPath.row 

      self.performSegueWithIdentifier("MainToLocation", sender: self) 
      //print(buildings[row].0) 
     } 

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

      if segue.identifier == "MainToLocation" { 
       let locationViewController = (segue.destinationViewController as! LocationViewController) 
       locationViewController.building = self.buildings[self.goToIndex!] 
      } 

     } 







    extension ViewController: UISearchResultsUpdating { 
     func updateSearchResultsForSearchController(searchController: UISearchController) { 
      filterContentForSearchText(searchController.searchBar.text!) 
     } 
    } 
+0

私の一時的な解決策はただviewTableがnil ... – user1990406

+1

ある場合は、あなたがセルをクリックして言っているチェックして、prepareForSegueが呼び出されます、そしてあなたこのビューコントローラviewDidLoadにエラーが発生しましたか?次に、あなたが思っているビューコントローラではなく、あなたのセグにこのビューコントローラをロードする必要があります。そうでない場合は、問題の説明が明確ではありません。 – Gruntcakes

+2

あなたはviewDidLoadのUI要素にアクセスしているので、まだ作成されていない可能性があります。 viewWillAppearまたはviewDidAppearメソッドにアクセスして、要素が実際に作成されたことを確認してください。 –

答えて

0

あなたはこれを試すことができます。..

let destinationVC = self.storyboard!.instantiateViewControllerWithIdentifier("viewController") as! NextViewController 


     var alreadyPushed = false 

     if let vc = self.navigationController?.viewControllers { 
      for viewController in vc { 
       if let viewController = viewController as? NextViewController { 
        self.navigationController?.popToViewController(viewController, animated: true) 
        print("Push your controller") 
        alreadyPushed = true 
        break 
       } 
      } 
     } 
     if alreadyPushed == false { 
      self.navigationController?.pushViewController(destinationVC, animated: true) 
     } 
関連する問題