简体   繁体   English

为什么我的tableView变量返回nil?

[英]Why is my tableView variable returning nil?

I am using a UISearchController in my project. 我在项目中使用UISearchController I initiate the search controller by supplying the init(searchResultsController) method with an UIViewController object that manages a tableView. 我通过为init(searchResultsController)方法提供一个管理tableView的UIViewController对象来启动搜索控制器。 The code for this object is: 该对象的代码是:

import UIKit

class ResultsTableViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
var list: [String] = []

 override func viewDidLoad() {
     super.viewDidLoad()
 }
}

extension ResultsTableViewController: UITableViewDelegate{}

extension ResultsTableViewController: UITableViewDataSource{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return list.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell")

     if cell == nil {
         cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
         cell?.textLabel?.text = list[indexPath.row]
      } else {
         cell?.textLabel!.text = list[indexPath.row]
      }
       return cell!
     }
}

Furthermore, when I try to access the resultsTableViewController.tableView from the updateSearchResultsForSearchController(searchController: UISearchController) method of the UISearchResultsUpdating protocol to populate its "list" array, it gives me an error. 此外,当我尝试从UISearchResultsUpdating协议的updateSearchResultsForSearchController(searchController: UISearchController)方法访问resultsTableViewController.tableView填充其“列表”数组时,它给了我一个错误。 The tableView returns nil and the app crashes. tableView返回nil并且应用程序崩溃。 I would like to point out that I have connected the data source, delegate, and the IBOutlet variable of the tableView to the appropriate view controller. 我想指出的是,我已将tableView的数据源,委托和IBOutlet变量连接到适当的视图控制器。 I was hoping for someone to explain to me why this happens? 我希望有人向我解释为什么会这样? I think I have a misunderstanding in the life cycle of the ResultsTableViewController . 我认为我对ResultsTableViewController的生命周期有误解。 Lastly, when I drag a TableViewController from the storyboard instead of making my own table view controller from scratch everything works smoothly without any errors! 最后,当我从情节提要中拖动TableViewController而不是从头开始制作自己的表格视图控制器时,一切都会顺利进行而不会出现任何错误! Can you please help me understand whats going on here? 你能帮我了解这里发生了什么吗?

Edit: The code for my initial view controller is: 编辑:我的初始视图控制器的代码是:

import UIKit

class FirstViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var resultsTableViewController  = ResultsTableViewController()
    var searchController: UISearchController!
    let list  = ["Apple", "Orange", "Bananas","Kiwi", "Cucumbers", "Apricot","Peach", "Cherry", "Mangosteen","Strawberry", "Blueberry", "Raspberry","Watermelon", "Persimmon", "plums","Papaya", "Jackfruit", "Lichi"]
    var filteredList: [String]!


    override func viewDidLoad() {
        super.viewDidLoad()
        setUpSearchController()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func setUpSearchController(){
        searchController = UISearchController(searchResultsController: resultsTableViewController)
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchResultsUpdater = self
        searchController.hidesNavigationBarDuringPresentation = true
        tableView.tableHeaderView = searchController.searchBar
        tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
    }

}

extension FirstViewController: UITableViewDelegate, UITableViewDataSource{

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       return list.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("cell")
        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
            cell?.textLabel?.text = list[indexPath.row]
        }else {
            cell?.textLabel?.text = list[indexPath.row]
        }
        return cell!
    }

}

extension FirstViewController: UISearchResultsUpdating{

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        filteredList = list.filter({
            item in
            return item.containsString(searchController.searchBar.text!)
        })
        resultsTableViewController.list = filteredList
        resultsTableViewController.tableView.reloadData()
    }
}

var resultsTableViewController = ResultsTableViewController()

creates a new ResultsTableViewController but it isn't linked to your storyboard scene, so none of the @IBOutlet s will be set. 创建一个新的ResultsTableViewController ,但它没有连接到你的故事板的场景,所以没有的@IBOutlet旨意进行设置。 You need to set an identifier for your scene (say resultsViewController ) and then use that to instantiate the view controller from the storyboard: 您需要为场景设置一个标识符(例如resultsViewController ),然后使用该标识符从情节提要中实例化视图控制器:

var resultsTableViewController: ResultsTableViewController!

override func viewDidLoad() {
    super.viewDidLoad()
    setUpSearchController()
}

func setUpSearchController(){

    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    self.resultsTableViewController = storyboard.instantiateViewControllerWithIdentifer("resultsViewController") as! ResultsTableViewController

    searchController = UISearchController(searchResultsController: resultsTableViewController)
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = true
    tableView.tableHeaderView = searchController.searchBar
    tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM