2017-02-14 71 views
0

我試圖創建2個不同的默認視圖控制器,然後調用,並根據單元格中的內容執行其指定的操作。你會在主故事板中使用xib等創建視圖控制器嗎?Swift - 如何實現defaultviewcontroller循環

基本上我有一個XML文件,我解析,然後創建表視圖,直到我到達項目的最後(詳細)頁面。根據我的理解,最好多次使用相同的tableviewcontroller,而不是爲每個級別創建1。我應該創建一個循環循環嗎?

有檢查,以確保我使用正確的賽格。

你pefrom與SEGUE:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath as IndexPath) 
    tableView.deselectRow(at: indexPath as IndexPath, animated: true) 
    segueLocation = hNTUL[(indexPath as NSIndexPath).row].locationLevel 
    segueTitle = hNTUL[(indexPath as NSIndexPath).row].locationTitle 
    if segueLocation == "TableView" { 
     performSegue(withIdentifier: "defaultDetailSeague", sender: cell) 
     let destinationVC = DefaultDetailViewController() 
     destinationVC.detailTitle = segueTitle 
    } 
    if segueLocation == "DetailView" { 
     performSegue(withIdentifier: "defaultTableSeague", sender: cell) 
     let destinationVC = DefaultTableViewController() 
     destinationVC.tableTitle = segueTitle 
    } 
} 

這也可能是我試圖去了解這個錯誤的方式。

我也一直在尋找項目的例子,但沒有找到任何可能引導我在正確的方向。

想法?

+0

感謝您展示更多代碼,但我的答案仍在下面。你需要一個'prepare(for:sender:)'的實現,而你沒有一個。請仔細閱讀,我在回答中說了什麼。 – matt

+0

這是你在說什麼? '重寫FUNC製備(對於SEGUE:UIStoryboardSegue,發件人:任何){ 如果segue.identifier == 「LevelTwo」{ 如果讓indexPath = self.tableView.indexPathForSelectedRow { 令控制器= segue.destination如! LevelTwoTableViewController 讓mCaty = mainCat [(indexPath爲NSIndexPath).row] controller.selectedName = mCaty.mainCategoriesTitle }} } ' – Number1

+0

是的,剛纔那類的事情,但我不認爲你的測試'自我.tableView.indexPathForSelectedRow'會按照你的預期工作(我可能會錯)。 – matt

回答

0

如果您致電performSegue,您必須而不是創建一個destinationVC;賽格爲你創造它。到配置目標視圖控制器,執行prepare(for:sender:)並獲取segue的destination,將其轉換爲DefaultDetailViewController。一定要先檢查一下這是否合適!

+0

我只能對代碼發表評論你表現出來。如圖所示,這是錯誤的,我的答案告訴你爲什麼。 – matt