2017-02-10 40 views
0

我正在使用Xcode 8.2Swift 3iOS 10。我意識到標題可能很難理解,所以讓我解釋我想要做的事情。如何在點擊按鈕後跳轉到細節時爲UITableViewCell創建細節?

我有一個TabViewController與三個子女ViewControllers,每個都有自己的NavigationController和其他東西之後。下面顯示了一個這樣的選項卡(選項卡1)。這是一個TableViewController,我在其上顯示我的自定義TableView單元的信息。

enter image description here

在不同的選項卡(例如,標籤2),我添加到細胞上按鈕點擊標籤1。我已經得到這個工作很好。現在,我想要的行爲是,當用戶單擊同一按鈕(在選項卡2上)時,我想添加一個單元格(與以前一樣),但也添加該單元格的詳細信息視圖並跳轉到該詳細信息視圖(而不是必須切換到選項卡1並單擊相應的單元格)。在這個詳細信息視圖中,我希望有一個返回按鈕,它會將我返回到選項卡1,這是訪問所有詳細信息視圖的位置。

我一直在試圖找出正確的方式來實現這一行爲,所以任何幫助將不勝感激。謝謝!

編輯

我提出基於以下關冬天的答覆的一些編輯。但是,我仍然無法將新創建的UIViewController與表格視圖單元「連接」。

我做了以下內容:

在表2:

// this code is located in a function where I call to create a new custom cell in Tab 1. 

//Switch to first tab 
tabBarController?.selectedIndex = 0 

//Show the detail view 
if let navigationController = tabBarController?.viewControllers?[0] as? UINavigationController { 
    let detailViewController = UIViewController() 
    detailViewController.view.backgroundColor = UIColor.red 
    navigationController.pushViewController(detailViewController, animated: true) 

    let tempNavVC = self.tabBarController?.viewControllers?[0] as! UINavigationController 
    let resultsTab = tempNavVC.viewControllers[0] as! Tab1VC 
    resultsTab.detailVCArray.append(detailViewController) 
} 

在表1:

// this is an array that holds the newly created view controllers passed in from Tab 2 
// when the user clicks on a cell, look up which cell it clicked on and display the view controller for that cell 
var detailVCArray = [UIViewController]() 

// called when a cell is clicked 
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let newChildVC = detailVCArray[indexPath.row] 
    self.addChildViewController(newChildVC) 
    self.present(newChildVC, animated: true, completion: nil) 
} 

當我運行它,如表2的代碼執行成功,我帶有紅色背景的新視圖控制器。但是,如果我點擊表視圖細胞,它的錯誤了:

「NSInvalidArgumentException」的,理由是:「應用程序試圖segueing以相同的紅色觀的提出 模態啓動控制

代替控制器。

我意識到這可能是做一個真正愚蠢的方式,但我想不出別的。

在一天結束的時候,我希望我的標籤2創建於表1中包含了一些視圖控制器與細節的表視圖單元和「過渡」我到這些細節。

+0

如果要創建小區信息TAB2那麼你有你想要顯示它的DetailView所有的細節。對? –

+1

聽起來像你想'UISplitViewController',而不是'UITabViewController'。 – NRitH

+0

添加單元格的代碼在哪裏? – Callam

回答

2

的代碼是這樣的:

//Click function in viewController of Tab 2 
func click(_ sender: Any) { 
    //Adding cells into Tab 1 

    //Switch to Tab 1 
    tabBarController?.selectedIndex = 1 

    //Show the detail view 
    if let navigationController = tabBarController?.viewControllers?[1] as? UINavigationController { 
     let detailViewController = UIViewController() 
     detailViewController.view.backgroundColor = UIColor.white 

     navigationController.pushViewController(detailViewController, animated: true) 
    } 
} 

//Code in viewController of Tab 1 
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    //For example: need to show detail view when row is 1 
    if indexPath.row == 1 { 
     //Show the detail view 
     let detailViewController = UIViewController() 
     detailViewController.view.backgroundColor = UIColor.white 

     navigationController?.pushViewController(detailViewController, animated: true) 
    } 
} 
+0

謝謝,冬天。這可以幫助我解決很多問題,但我仍然試圖弄清楚如何將新創建的View Controller「連接」到我的Table View Cell。我編輯了我原來的帖子。 – noblerare

+0

您應該在'override func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)'中使用類似的代碼來顯示詳細視圖。 – Winter