Swift UITableView didSelectRowAtIndexPath没有被调用

IOS开发新手,在处理表格中的单元格select时遇到问题。 每当我select,方法不被调用下面 – 任何想法,为什么?

我的项目结构是:View Controller – > View – > Table View

下面的代码演示了方法调用。 别人被叫没问题! 我知道触摸正在拉下成功刷新和点击一个单元格,它确实变得突出显示。

import UIKit class ViewController: UIViewController, UITableViewDelegate { let blah = ["blah1"] //How many sections are in the table? func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } //How many rows? (returns and int) func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return blah.count } //table contents for each cell? //Each time this is called it'll return the next row and thus build a table... func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { print("Populating each cell of table view!\n") tableView.rowHeight = 80.0 var cell = UITableViewCell() var(a) = blah[indexPath.row] var image : UIImage = UIImage(named: a)! cell.imageView.image = image return cell } //Code Cell Selected func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ println("You selected cell #\(indexPath.row)!") } func tableView(tableView: UITableViewDelegate, didDeselectRowAtIndexPath indexPath: NSIndexPath) { print("wananananaanan" ) println("You deselected cell #\(indexPath.row)!") } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

你必须在你的ViewControllertableView设置一个@IBOutlet ,并设置它的delegatedataSource ,你可以看到数据响应tableView变化。

像这样的东西:

 override func viewDidLoad() { super.viewDidLoad() self.tableView.delegate = self self.tableView.dataSource = self } 

并且也实现了UITableViewDataSource协议。

或者你也可以在界面生成器中设置ViewController因为它的委托和dataSource (我认为更容易),并避免像上面的代码手动设置。 你决定。

我希望这可以帮助你。

SWIFT 3

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // Do here } 

在swift中使用上面的委托方法3

在比较两个相同的代码示例时,我遇到了同样的问题,其中一个工作正常,另一个不调用didSelectRowAtIndexPath

看看解决这个问题的两种可能的方法:

1)在代码本身:

 @IBOutlet weak var table: UITableView! override func viewDidLoad() { table.delegate = self table.dataSource = self //data source might be already set if you see contents of the cells //the main trick is to set delegate } 

2)使用故事板或文档大纲(这是我的情况下的问题导致故事板更改在.swift控制器类中不可见。

打开文档大纲和Control + Press你的TableView你会看到两个名为“ delegate ”和“ dataSource ”的出口,将它们一个一个地拖到包含的ViewController (右边到黄色圆圈)

而已!

你必须使用这个:首先看看你在扩展什么,然后使用tableView方法。

  class YourViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var mUITableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // We need to tell to UITableView that we will add the data by ourselves self.mUITableView.delegate = self self.mUITableView.dataSource = self // Register the UITableViewCell class with the tableView self.mUITableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.cellIdentifier) // Setup table data getEvents() self.mUITableView.allowsSelection = true } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableData.count } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { // here to create you cell view } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("You selected cell #\(indexPath.row)!") } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "subtitleCell") cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator cell.textLabel?.text = "\(tableData[indexPath.row].name) - (\(tableData[indexPath.row].eventStateId))" cell.detailTextLabel?.text = tableData[indexPath.row].lastUpdate return cell } }