Vivek Sharma
Vivek Sharma

Reputation: 31

Uable to trigger the action on pressing table cell in swift

I am unable to trigger an action as i click on the table cell. The background of the pressed cell is just gray after pressing the cell. But the action is not performing. Please help.

import UIKit

class ViewController: UIViewController, UITableViewDataSource , UITableViewDelegate 
{
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    var loki_images:[UIImage] = [UIImage(named: "images.png")!,
                                UIImage(named: "images (1).png")!,
                                UIImage(named: "images (2).png")!]

    var persons = [ "Lokesh Ahuja" , "Raghav Mittal" , "Tul-Tul" ]

    var identities = ["A","B","C"]

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

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")

        let Name   = persons[indexPath.item]

        cell!.textLabel!.text = Name

        let image   = loki_images[indexPath.row]

        cell!.imageView?.image = image

        return cell!
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let id = identities[indexPath.item]
        let viewController = storyboard?.instantiateViewControllerWithIdentifier(id)
        self.navigationController?.pushViewController(viewController!, animated: true)
    }
}

Upvotes: 0

Views: 54

Answers (2)

Hiren
Hiren

Reputation: 250

Have your proparly connected Table's UITableViewDelegate with View Controller in Storyboard. Check it. If you not connect it than didselect will not called.

enter image description here

Upvotes: 0

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

Are your UIViewController is in navigation controller

if not then you should replace

self.navigationController?.pushViewController(viewController!, animated: true)

with

self.present(viewController, animated: true, completion: nil)

Upvotes: 2

Related Questions