ios - 加载表格时 tableView outlet nil

标签 ios uitableview swift

我有一个 tabBarController 和两个 UINavigationControllersUITableViews 附加到 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/

相关文章:

ios - 使用 Storyboard的 UIPageViewController 示例

ios - clearsSelectionOnViewWillAppear 有什么作用?

iOS UITableViewCell 取消选择问题

ios - 带有 "Prefers Large Titles"和嵌入式 PageViewController 的导航栏不折叠

ios - Apple 是如何做到这样的?

ios - UIColor,iPhone 5 与 iPhone 4

ios - 将 UI 滑动手势识别添加到自定义 View ios

ios - 如何快速解决桥接头错误

SwiftUI 我想使用 simultaneousGesture 但也想使用孤立的手势

android - 快速检测信标是否超出范围