tableView outlet nil在加载表时

时间:2015-04-18 15:36:57

标签: ios uitableview swift

我有一个tabBarController和两个UINavigationControllers UITableViews附加到tabBarController。我已经将tabBarController子类化,因此我可以通过引用在两个表之间传递一组自定义对象。但是,当我去填充数组时,tableView出口打开到nil并且我收到错误“致命错误:在展开Optional值时意外地发现了nil”。不知道为什么会这样。以下是代码:

tabBarController

protocol CustomTabBarDelegate {
var placeDataArray : Array <placeData> {get set}
}

class placeData: Equatable {
var description : String
var selected : Bool

init (description : String, selected : Bool) {
    self.description = description
    self.selected = selected
}
}

class PlacesTabBarController: UITabBarController {

var placeDataArray = Array<placeData>()

override func viewDidLoad() {
    super.viewDidLoad()

    placeDataArray = [placeData(description: "Afghanistan", selected: false), placeData(description: "Albania", selected: false)]

    var table1 = AllPlacesViewController()
    var table2 = AttendingPlacesTableViewController()

    table1.delegate = self
    table2.delegate = self

    var navController1 = UINavigationController(rootViewController: table1)
    var navController2 = UINavigationController(rootViewController: table2)

    self.viewControllers = [navController1, navController2]
    }
}

tableViewControllers

var delegate:PlacesTabBarController!

然后,例如,我可以通过调用

来访问该数组
delegate.placeDataArray.count

当我用

填充表格时
@IBOutlet weak var allPlacesTableView: UITableView!

并使用

 var cell = self.allPlacesTableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell

抛出上述错误。不确定allPlacesTableView为什么要解包为

2 个答案:

答案 0 :(得分:6)

当您像这样创建VC时,

var table1 = AllPlacesViewController()

它们与故事板没有关联,因此没有实现这些出口。请改用self.storyboard的instantiateViewControllerWithIdentifier方法:

var table1 : AllPlacesViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("AllPlaces") as AllPlacesViewController

答案 1 :(得分:0)

可能是您没有正确实例化ViewController。确保如果要从storybaord实例化VC,则必须这样做:

let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let someVC = storyboard.instantiateViewController(withIdentifier: "SomeViewController") as? SomeViewController