Stacy J
Stacy J

Reputation: 2781

How to detect if a table view cell is tapped in xcode 7

How do I detect if a table view cell is tapped. The guides and tutorial on the web follow the approach of adding a button to the table cell view - prototype cell. This button remains same for all rows and when tapped, returns the index of the row or cell.

I want to do this without the button. How can I invoke

@IBAction

when a cell is tapped and then perform a segue inside the function.

How should I go about it?

I added the following code, but this does not print anything

class MoreViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let appDelegate  = UIApplication.sharedApplication().delegate as! AppDelegate

let moreOption = [
    ("My Profile"),
    ("Settings"),
    ("Logout")
]

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.navigationController!.navigationBar.barTintColor = UIColor(red: (48/255.0), green: (187/255.0), blue: (197/255.0), alpha: 1.0);
    self.navigationController!.navigationBar.tintColor = UIColor .whiteColor();
    self.navigationController!.navigationBar.translucent = false;
    self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor .whiteColor()]
}

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

func logout() {
    print("Logout tapped");
}


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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
    let (celloption) = moreOption[indexPath.row];
    cell.textLabel!.text = celloption;
    cell.textLabel!.textColor =  UIColor(red: (74/255.0), green: (74/255.0), blue: (74/255.0), alpha: 1.0);
    return cell;
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    print(cell);
}

}

Upvotes: 0

Views: 1552

Answers (3)

Noel Carcases
Noel Carcases

Reputation: 721

On the code you are not adding the tableview outlet (or variable if you are building it from code). You need first to add the tableview outlet to your class and then set the apropiate delegate and datasource for it

for example

//this is connected to your storyboard 
//It may have typing errors it is not tested 
//at this on the top of your class
@IBOutlet weak var table: UITableView! 

//at this on viewdidLoad or viewDidAppear 
table.delegate = self
table.datasource = self

Upvotes: 0

iDeveloper
iDeveloper

Reputation: 2444

Try This

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            let selectedItem = items.objectAtIndex(indexPath.row) as String
            print(selectedItem)
        }

Upvotes: 0

PGDev
PGDev

Reputation: 24341

Why aren't you using UITableViewDelegate method?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

In this method you will get the indexPath of cell tapped and here itself you can open the new controller if you want.

Upvotes: 3

Related Questions