0

I have created 2 TableViewControllers in one ViewController programmatically as :

// contents for labels in cells for tableviewcontrollers

let contents1 : [String] = ["One:","Two:","Three:","Four:","Five:"]

let contents2 : [String] = ["Six:","Seven:","Eight:","Nine:","Ten:"]

override func viewDidLoad() {
    super.viewDidLoad()

    table1.delegate = self
    table1.dataSource = self

    table2.delegate = self
    table2.dataSource = self   
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(tableView.tag == 1)
    {
        return contents1.count
    }
    else if (tableView.tag == 2)
    {
        return contents2.count
}
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  let cell = tableView.dequeueReusableCell(withIdentifier: "cell",  for: indexPath as IndexPath )

    if (tableView.tag == 1)
    {
        cell.textLabel?.text = contents1[indexPath.row]
    }
    else if (tableView.tag == 2)
    {
        cell.textLabel?.text = contents2[indexPath.row]

    }
return cell
}

My question is that , how can I programmatically link "Four:" label of first TableViewController "table1" when selected to show up next new ViewController without using Segue?

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
Angel Kajol
  • 45
  • 1
  • 7
  • if you do not want to use segue then you can use NSUserDefaults and Create Delegate method – Mayank Patel Dec 19 '16 at 10:07
  • Mayank Patel @ Sir, I am beginner at Swift. Can you please elaborate the solution since I have given whole codes? – Angel Kajol Dec 19 '16 at 10:17
  • You want to pass data to next view controllor right ? – Mayank Patel Dec 19 '16 at 10:17
  • Mayank Patel @ Not data, just show up next ViewController after pressing "Four:" from table1 TableViewController programmatically. – Angel Kajol Dec 19 '16 at 10:28
  • you can push view controller [Here how you can do] - http://stackoverflow.com/a/24038621/3800154 – Mayank Patel Dec 19 '16 at 10:30
  • Mayank Patel @ Sir, I didn't ask about the content in this link, I need to show up New ViewController while pressing "Four:" of table1 ...,,and I need to show up next new different ViewControllers while pressing "One:", "Two:", Three:". By the technique of link , it shows up same ViewControllers while pressing "One:", "Two:", Three:"Four:"..... – Angel Kajol Dec 19 '16 at 10:37
  • posted answer check it – Mayank Patel Dec 19 '16 at 10:44

2 Answers2

0

You can UITableView's deSelectRowAt delegate method to identify the cell selection after that you can proceed to next view

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)

    if cell?.textLabel?.text == "Four" { // you can use you indexPath to compare as well
        let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
        self.navigationController.pushViewController(secondViewController, animated: true)
    }
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
  • Mayank Patel @ Thanx Sir, It worked for self.present(secondViewController, animated: true, completion: nil) but it didnot work for self.navigationController?.pushViewController(secondViewController,, animated: true) . It didnot push up to SecondViewController but i wanted to push with navigation controller as i need default Back button.Can you plzz help here ?? – Angel Kajol Dec 19 '16 at 11:12
  • You have to embed your current view controllor with navigation controllor after that you were able to push in secondviewcontrollor – Mayank Patel Dec 19 '16 at 11:14
  • You don't need to check the cell against nill, if you do optional unwrapping – Lefteris Dec 19 '16 at 11:17
  • Mayank Patel@ Thanx, it helped me. – Angel Kajol Dec 19 '16 at 11:53
-1
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = pets[indexPath.row]
    // Configure the cell...

    return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex = indexPath.row
    performSegue(withIdentifier: "segue", sender: self)
cbuchart
  • 10,847
  • 9
  • 53
  • 93