Manik Khurana
Manik Khurana

Reputation: 39

Going to another view when I click a tableview cell

I know this question has been asked before but all the solutions I have found have really not made sense to me. I am a beginner at iOS and I know how I can left click and drag a button to another view to make a transition, but I can not do it with the tableview. My layout is just two view controllers, one with a tableview and the second is blank, and I would eventually want to update labels for each tableview cell click.

The code below is for the first view controller with everything to make a basic tableview work

import UIKit

var fullList = [String]()
var dateList = [String]()

class ViewController: UIViewController, UITableViewDelegate {

    @IBOutlet var betTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if(NSUserDefaults.standardUserDefaults().objectForKey("fullList") != nil)   //check if the to do list exists
        {
            fullList = NSUserDefaults.standardUserDefaults().objectForKey("fullList") as! [String]      //store toDoList array into NSUserDefaults to save to iphone
        }

        if(NSUserDefaults.standardUserDefaults().objectForKey("dateList") != nil)
        {
            dateList = NSUserDefaults.standardUserDefaults().objectForKey("dateList") as! [String]
        }
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        cell.textLabel?.text = fullList[indexPath.row]
        return cell
    }

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

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
    {
        if(editingStyle == UITableViewCellEditingStyle.Delete)
        {
            fullList.removeAtIndex(indexPath.row)
            NSUserDefaults.standardUserDefaults().setObject(fullList, forKey: "fullList")
            NSUserDefaults.standardUserDefaults().setObject(dateList, forKey: "dateList")

            betTable.reloadData()
        }
    }

    override func viewDidAppear(animated: Bool)
    {
        betTable.reloadData()
    }
}

Upvotes: 1

Views: 107

Answers (1)

bhakti123
bhakti123

Reputation: 853

  1. Drag from your tableViewController to the SecondViewController to create a Segue. (like This)

enter image description here

Select the type according to your requirement.

  1. Now select the Segue and in the attributes inspector, give any name to the field 'Identifier'. We will use this later for referencing purposes.

enter image description here

  1. Now in your ViewVontroller.swift file, navigate to function didSelectRowAtIndexPath. Inside this you can call the func performSegueWithIdentifier and provide the name you gave earlier. Here is the code:

enter image description here

Hope this helps. :)

Upvotes: 1

Related Questions