如何在UITableViewDelegate的子类UIViewController在很多子类

问题描述:

我想有一个可重用的视图控制器调用MainViewController与UITableView里面。如何在UITableViewDelegate的子类UIViewController在很多子类

class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet var tableView : UITableView! 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     self.tableView.dataSource = self 
     self.tableView.delegate = self 
     self.tableView.scrollsToTop = true 

     self.tableView.estimatedRowHeight = 124.0 
     self.tableView.rowHeight = UITableViewAutomaticDimension 
} 

MainViewController

我需要我的MainViewController的很多子类,以定制可以根据他们的我的需要。 IntroViewController就是其中之一。

class IntroViewController: MainViewController { 

} 

要打开IntroViewController,在这里我赛格瑞:

UINavigationController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if segue.identifier == "intro" { 

     let destination = segue.destination as! UINavigationController 
     let ivc = destination.topViewController as! IntroViewController 
    } 

} 

我得到这个崩溃:

fatal error: unexpectedly found nil while unwrapping an Optional value 

为线

self.tableView.dataSource = self 

我查过了,我的店铺连接正确。数据源也是。

+0

什么是StoryViewController? –

+0

MainViewController,我改正了。 – cmii

+0

为什么导航控制器之间的2个控制器。? –

你想要做的事情并不真的那样。 @IBOutlets全部通过故事板连接 - 但它们不会“流过”您希望使用的模式。

可以做到这一点 - 在某种程度上。看看一个讨论和解答这个SO职位:How to subclass custom UIViewController in Swift?

另外,如果你的目标是用一个“共同”表视图控制器 - 但允许定制 - 你可以创建单独的UITableViewDelegateUITableViewDataSource文件,再加上分开视觉设计的细胞,然后根据需要通过代码分配它们。