如何检测tableView单元格在快速触摸或点击

我试图在TableView获取所选项目的index ,然后开始一些活动。 不幸的是,我发现的大多数解决scheme都是在objective-c或不工作。

方法func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)不打印cell标签..

有人可以帮我吗?

导入UIKit
导入ResearchKit

类TaskListViewController:UIViewController,UITableViewDataSource {

    让任务= [(“短步行”),
         ( “测听”),
         (“手指敲击”),
         (“反应时间”),
         (“空间跨度内存”)
     ]


     //表格中有多less部分
     func numberOfSectionsInTableView(tableView:UITableView) - > Int {
        返回1
     }

     //返回int多less行
     func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int {
        返回tasks.count
     }

     //内容是什么

     func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {
         var cell = UITableViewCell()

         var(testName)= tasks [indexPath.row]
         cell.textLabel?的.text =testing名
        返回单元格
     }

     //给每个表部分一个名字

     func tableView(tableView:UITableView,titleForHeaderInSection部分:Int) - > String?  {

        返回“任务”

     }

     func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){

         let indexPath = tableView.indexPathForSelectedRow();

        让currentCell = tableView.cellForRowAtIndexPath(indexPath!)作为UITableViewCell!

        的println(currentCell.textLabel!的.text)
     }


    重写func viewDidLoad(){
         super.viewDidLoad()

     }  
 }

经过几次尝试,我将代码更改为另一个来自我发现的教程。 而且这也不起作用。 现在我想这是iOS模拟器的问题…

导入UIKit
导入ResearchKit

类TaskListViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {

     @IBOutlet
     var tableView:UITableView?
     var items:[String] = [“We”,“Heart”,“Swift”]

    重写func viewDidLoad(){
         super.viewDidLoad()

         self.tableView!.registerClass(UITableViewCell.self,forCellReuseIdentifier:“cell”)
     }


     func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int {
        返回self.items.count;
     }

     func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {
         var cell:UITableViewCell = self.tableView!.dequeueReusableCellWithIdentifier(“cell”)as! 的UITableViewCell

         cell.textLabel?.text = self.items [indexPath.row]

        返回单元格
     }

     func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){
         println(“你select了单元格#\(items [indexPath.row])!”)
     }

 }

如果你想从单元格的值,那么你不必在didSelectRowAtIndexPath重新创build单元格

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { println(tasks[indexPath.row]) } 

任务如下:

 let tasks=["Short walk", "Audiometry", "Finger tapping", "Reaction time", "Spatial span memory" ] 

你也必须检查cellForRowAtIndexPath你必须设置标识符。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell var (testName) = tasks[indexPath.row] cell.textLabel?.text=testName return cell } 

希望能帮助到你。

在Swift 3.0中

你可以通过它的委托方法find触摸/点击tableview单元格的事件。 同样,可以像这样find单元格的行和列的值。

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("section: \(indexPath.section)") print("row: \(indexPath.row)") } 

问题是由我自己使用weheartswift教程解决的

在这里输入图像说明

这对我有好处:

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("section: \(indexPath.section)") print("row: \(indexPath.row)") }