【问题标题】:iOS Swift didSelectRowAtIndexPath not workingiOS Swift didSelectRowAtIndexPath 不起作用
【发布时间】:2016-02-09 18:51:55
【问题描述】:

当用户在我的 TableView 中选择一个单元格时,我想执行一行代码。在运行时,我的 TableView 可以填充数据,并且单元格确实是可点击/可选择的。但是,当一个单元格被选中时,'didSelectRowAtIndexPath 方法被跳过/不被调用。我是 Swift 的新手,所以也许我忽略了一些非常简单的东西。我的代码结构如下:

import UIKit

class TopicsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

//Number of Sections:
func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 2 }

//Number of Rows in Each Section:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int { return 3 }

//Contents of Each Cell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell()
    cell.textLabel?.text = "Data"
}

//Code below is skipped/ignored at runtime, no matter what I try:  
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
print("Hello!") 
}

注意:我没有使用手势识别器(这个主题的其他线程表明手势控制器可能是罪魁祸首)

【问题讨论】:

  • 您是否在 cellForRowAtIndexPath 中返回单元格?
  • 表格视图是以编程方式添加的还是在情节提要中添加的?以编程方式/情节提要:tableView.delegate = self 或在情节提要中检查委托是否已设置。
  • 您是否将表 dataSource AND delegate 出口连接到您的视图控制器?似乎有效的方法是dataSource 的方法,而无效的方法是delegate
  • 谢谢大家! EmilioPelaez 是对的——我在视图控制器类中包含了委托协议,但我没有意识到我还需要将委托出口连接到视图控制器。现在似乎一切正常。

标签: ios swift didselectrowatindexpath


【解决方案1】:

您是否分配了 tableview 的“代表”?

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    相关资源
    最近更新 更多