Rajamohan S
Rajamohan S

Reputation: 7269

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

Hi all. I'm troubling with tableView in swift. Actually i created the table view with two rows(About and Login) in main view controller. Problem at initial, when i click the about or Login , then the New View controller is not opened. But, I try second time to another one row, then the first clicked view Controller is opened at this time of Clicking. so, this cycles shown wrong view controller at every time of clicking. Please tell me as whats wrong with my Code?? Please refer screenshot given below.Thanks in advance!!!

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()

    }


}

Below Screen shown, I clicked About Row. But, the Login ViewController is Opened.

enter image description here

This is my Story Board Connections. enter image description here

Upvotes: 0

Views: 995

Answers (1)

Chandan
Chandan

Reputation: 757

Just update didSelectRowAtIndexPath method

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
    }
}

Upvotes: 1

Related Questions