1

i am pretty new to ios development. I have a table view with 2 Sections

TableView:

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
var people = ["ABC","PQR","LMN"]
var languages = ["Android","Java","C++",".Net"]


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

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section==0
    {return people.count}
    else
    {return languages.count
    }
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    if indexPath.section==0{
        cell.textLabel?.text = people[indexPath.row]
    }
    else{
        cell.textLabel?.text = languages[indexPath.row]

    }
    return cell
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section==0{
        title="Names"
    }
    else{
        title="Languages"
    }
    return title
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
 }

I want make each item in the table view clickable. That is when an item from the table view is clicked a new corresponding table view opens. Can someone help me with it ?

Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54

2 Answers2

0

All you need if a if/switch statement in your didSelectRowAtIndexPath section. For example :

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    if (indexPath.row == 0) {
        //do something
    }
    else if (indexPath.row == 0) {
        //do something else
    }
}

According the fact you want a new table view after click, I think you should perform a segue to present a new view controller. See here : How to segue programmatically using swift

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
AnthoPak
  • 4,191
  • 3
  • 23
  • 41
  • Thanks. But what do i write in the if statement. Like how do i navigate to the new table view? – Khadija Daruwala Jun 17 '16 at 09:08
  • That's the reason why I added a link for you on how to perform segues. You need to have your other tableviews in storyboard, give them a unique identifier, then use performSegueWithIdentifier with different identifier in if statement. – AnthoPak Jun 17 '16 at 09:12
0

You can achieve navigation on other controller like this,

First you need to embed Navigation controller with your view so you can navigate easily.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{
    if (indexPath.row == 0) 
    {

         let vc = self.storyboard?.instantiateViewControllerWithIdentifier("AddVC") as! addRecordViewController 
         self.navigationController?.pushViewController(vc, animated: true)
    }
    else
    {
       // Navigate on other view
    }
}

Hope this will help.

Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38