簡體   English   中英

如何在Swift 2.0中使用多個View Controller創建動態Table View單元格?

[英]How to create Dynamic Table View cells With multiple View Controller in swift 2.0?

大家好。 我很快就麻煩了tableView。 實際上,我在主視圖控制器中創建了具有兩行 (關於和登錄)的表視圖。 最初出現問題,當我單擊about或Login時, 未打開 New View控制器 但是,我第二次嘗試到另一行,然后在單擊時打開第一個單擊的視圖控制器。 因此,此循環在每次單擊時顯示錯誤的視圖控制器。 請告訴我我的代碼有什么問題嗎? 請參考下面的截圖 。謝謝!!!

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {


    @IBOutlet var tableView: UITableView!

    var titles = ["About","LogIn"]
    override func viewDidLoad() {
        super.viewDidLoad()

    }

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as! TableCell
        cell.label.text = titles[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        switch indexPath.row
        {
        case 0: self.performSegueWithIdentifier("aboutSegue", sender: self)
        case 1: self.performSegueWithIdentifier("loginSegue", sender: self)
        default: break
        }
    }

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

        if segue.identifier == "aboutSegue"
        {
          let vc = segue.destinationViewController as! about
            vc.title = "About"
        }
        else
        {
            let vc = segue.destinationViewController as! login
            vc.title = "Login"

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


}

在顯示的屏幕下方,我單擊了關於行。 但是,登錄ViewController已打開。

在此處輸入圖片說明

這是我的故事板連接。 在此處輸入圖片說明

只需更新didSelectRowAtIndexPath方法

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    switch indexPath.row
    {
    case 0: 
        self.performSegueWithIdentifier("aboutSegue", sender: self) 
        break;
    case 1: 
        self.performSegueWithIdentifier("loginSegue", sender: self)
        break;

    default: break
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM