我正在使用Xcode 8.2,Swift 3,iOS 10。我意識到標題可能很難理解,所以讓我解釋我想要做的事情。如何在點擊按鈕後跳轉到細節時爲UITableViewCell創建細節?
我有一個TabViewController
與三個子女ViewControllers
,每個都有自己的NavigationController
和其他東西之後。下面顯示了一個這樣的選項卡(選項卡1)。這是一個TableViewController
,我在其上顯示我的自定義TableView
單元的信息。
在不同的選項卡(例如,標籤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中包含了一些視圖控制器與細節的表視圖單元和「過渡」我到這些細節。
如果要創建小區信息TAB2那麼你有你想要顯示它的DetailView所有的細節。對? –
聽起來像你想'UISplitViewController',而不是'UITabViewController'。 – NRitH
添加單元格的代碼在哪裏? – Callam