2017-04-11 22 views
1

我想有一個可重用的視圖控制器調用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個控制器。? –

回答

0

你想要做的事情並不真的那樣。 @IBOutlets全部通過故事板連接 - 但它們不會「流過」您希望使用的模式。

可以做到這一點 - 在某種程度上。看看一個討論和解答這個SO職位:How to subclass custom UIViewController in Swift?

另外,如果你的目標是用一個「共同」表視圖控制器 - 但允許定製 - 你可以創建單獨的UITableViewDelegateUITableViewDataSource文件,再加上分開視覺設計的細胞,然後根據需要通過代碼分配它們。

相關問題