尝试离开tableView时发现nil

时间:2016-05-03 22:35:19

标签: ios swift uitableview null tableview

我在viewDidLoad中尝试引用viewTable时遇到错误我单击要转换为segue的单元格后。非常感谢!!

基本上我不能使用segue,除非我在视图中注释了tableview引用加载了...但是我需要那些才能使用搜索栏并且确定它会在回来的路上引起问题。 ..

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

1 个答案:

答案 0 :(得分: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)
        }