在ContainerView中使用UITableView和一个Prototype单元格 . 该表将需要提供单元格,以便在运行时,代码将在不同的单元格中添加不同的视图以供用户信息和交互 . tableView和containerView.swift之间的连接是它的dataSource和delegate .

在containerView.swift中,UIViewController已被扩展 .

extension myContainerView: UITableViewDataSource, UITableViewDelegate {...}

在该代码块中,需要func返回 numberOfRowsInSectioncellForRowAtIndexPath

但这些信息在运行时可用,因此我的代码现在不符合,当达到 return parent.mainTbRowCount() 时,我得到以下错误,如果我现在只返回一个虚拟int,当达到 let view = parent.cellsViewsDesider.views![indexPath.row] 时,我得到相同的错误 .

致命错误:在展开Optional值时意外发现nil

extension myContainer: UITableViewDataSource, UITableViewDelegate {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return parent.mainTbRowCount() // <------ fatal error
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("topCellId", forIndexPath: indexPath)
    let view = parent.cellsViewsDesider.views![indexPath.row]  // <---- fatal error 
    cell.contentView.addSubview(view!)
    return cell
}

如果我删除tableView和containerView.swift之间的连接作为其dataSource和委托,代码编译但 numberOfRowsInSection and cellForRowAtIndexPath 的函数永远不会被调用 .

在这种情况下需要做什么?

谢谢