2015-08-08 171 views
0

我有動態原型一個基本表視圖控制器,夫特 - 詳細視圖控制器根據表視圖細胞

那裏可以出現4個細胞,但也可出現6個細胞。

我要的是:

取決於哪個小區點擊切換到另一個「細節」 -ViewController。

我首先想到的是這樣的:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    if cell1clicked { 
     // go to viewcontrollerA 
    } elseif cell2clicked { 
     // go to viewcontrollerB 
    } elseif cell3clicked { 
     // go to viewcontrollerC 
    } else { 
     // go to viewcontrollerD 
    } 

} 

是這甚至可能與tableviewcontrollers?

問候和感謝!

+0

我已經更新了我的答案,它應該工作:) – AaoIi

回答

2

它可以通過在故事板中使用TableViewController來實現,並使用靜態單元而不是動態。

這裏是這樣的:

1-定義4個細胞在靜態的tableView。

2-在故事板中拖動4視圖控制器。

3-在Storyboard中將第一個單元與viewcontrollerA等連接起來。

4-確保你的類繼承UITableViewController並刪除tableview方法。

更新:

如果要使用動態細胞及其可能推ViewController編程:

1- 4個拖動和ViewControllers去身份檢查,並給每一個 一個故事板ID。

2-爲您的ViewControllers創建4個類,並將每個 故事板連接到它的類。 - 在 故事板中將導航控制器添加到您的初始ViewController。

伊夫讓你在這裏的樣本類:

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{ 


    var array = ["First","Second","Third","Fourth"] 


    @IBOutlet weak var tableviewmy: UITableView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 4 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell 


     cell.textLabel?.text = array[indexPath.row] 

     return cell; 
    } 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     if indexPath.row == 0 { 
      var controller1 : FirstViewController = storyboard?.instantiateViewControllerWithIdentifier("ViewControllerA") as! FirstViewController 
      self.navigationController?.pushViewController(controller1, animated: true) 

     }else if indexPath.row == 1{ 
      var controller2 : SecondViewController = storyboard?.instantiateViewControllerWithIdentifier("ViewControllerB") as! SecondViewController 
      self.navigationController?.pushViewController(controller2, animated: true) 
     }else if indexPath.row == 2 { 
      // controller 3 
     }else {  
      // controller 4 
     } 

    } 

} 

你可以下載一個示例項目: https://yadi.sk/d/F5Qb85MLiMPqk

+0

嘿感謝您的FPR回答,我在我的初始視圖控制器中添加了一個表視圖,並創建了一個樣式爲「basic」的自定義單元格。但表格視圖仍然是空的?你可以參考這個? –

+0

自定義單元格不適用於基本樣式,您必須將其更改爲自定義樣式,否則上傳樣本以向您展示。請檢查我的答案並下載樣本,@Creativecrypter – AaoIi

+0

讓我知道它是否有效,然後您可以接受它:) @Creativecrypter – AaoIi

相關問題