能够在swift中按下表格单元格时触发操作

时间:2017-08-24 07:18:09

标签: swift

我点击表格单元格时无法触发操作。压制细胞后压制细胞的背景仅为灰色。但行动没有表现。请帮忙。

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)
    }
}

2 个答案:

答案 0 :(得分:2)

你的UIViewController是否在导航控制器中

如果没有,那么你应该替换

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

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

答案 1 :(得分:0)

在Storyboard中将Table的UITableViewDelegate与View Controller进行预先连接。核实。如果你没有连接它,那么不会调用didselect。

enter image description here