我有一个 tabBarController
和两个 UINavigationControllers
,UITableViews
附加到 tabBarController
。我已经对 tabBarController
进行了子类化,因此我可以通过引用在两个表之间传递一组自定义对象。但是,当我去填充数组时,tableView
导出展开为 nil,我收到错误“ fatal error :在展开可选值时意外发现 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
展开为 nil
最佳答案
当您像这样创建 VC 时,
var table1 = AllPlacesViewController()
它们未链接到 Storyboard,因此未实现导出。使用 self.storyboard 的 instantiateViewControllerWithIdentifier
方法代替:
var table1 : AllPlacesViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("AllPlaces") as AllPlacesViewController
关于ios - 加载表格时 tableView outlet nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29719373/