它可以通過在故事板中使用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
我已經更新了我的答案,它應該工作:) – AaoIi