【问题标题】:Uable to trigger the action on pressing table cell in swift无法快速触发按下表格单元格的动作
【发布时间】:2017-08-24 07:18:09
【问题描述】:

当我单击表格单元格时,我无法触发操作。按下单元格后,按下单元格的背景只是灰色。但是动作没有执行。请帮忙。

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

【问题讨论】:

  • 您是否为您的 tableView 设置了委托 (tableView.delegate = self)?
  • 你能提供你的故事板的截图吗?

标签: swift


【解决方案1】:

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

如果不是,那么你应该更换

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

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

【讨论】:

    【解决方案2】:

    让您的 Table 的 UITableViewDelegate 与 Storyboard 中的 View Controller 正确连接。核实。如果您不连接它,则不会调用 didselect。

    【讨论】:

      猜你喜欢
      • 2020-04-10
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 2019-07-18
      • 2018-10-05
      • 2016-08-03
      相关资源
      最近更新 更多