如何检测在swift中触摸或单击的tableView单元格 [英] How to detect tableView cell touched or clicked in swift
问题描述
我正在尝试获取 TableView
中所选项目的 index
,然后启动一些活动。不幸的是,我发现大多数解决方案都是在objective-c中或者不起作用。
I'm trying to get index
of selected item in TableView
and start some activity after that. Unfortunately most of solutions that I found are in objective-c or do not work.
方法 func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath )
不要打印单元格
标签..
Method func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
don't print the cell
label..
有人可以帮我吗?
import UIKit
import ResearchKit
class TaskListViewController: UIViewController, UITableViewDataSource {
let tasks=[("Short walk"),
("Audiometry"),
("Finger tapping"),
("Reaction time"),
("Spatial span memory")
]
//how many sections are in your table
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//return int how many rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
//what are the contents
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
var (testName) = tasks[indexPath.row]
cell.textLabel?.text=testName
return cell
}
// give each table section a name
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Tasks"
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
println(currentCell.textLabel!.text)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
经过几次尝试后,我将代码更改为与教程不同的代码我发现。它也不起作用。现在我认为这是iOS模拟器的问题...
After a few tries I changed the code to a different one from tutorial that I found. And it doesn't work too. Now I'm thinking this is the issue with iOS simulator...
import UIKit
import ResearchKit
class TaskListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet
var tableView: UITableView?
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 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]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(items[indexPath.row])!")
}
}
推荐答案
如果你想要来自单元格的值,那么你不必重新创建单元格在 didSelectRowAtIndexPath
If you want the value from cell then you don't have to recreate cell in the didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println(tasks[indexPath.row])
}
任务如下:
let tasks=["Short walk",
"Audiometry",
"Finger tapping",
"Reaction time",
"Spatial span memory"
]
你还要检查 cellForRowAtIndexPath
你要哈哈设置标识符。
also you have to check the cellForRowAtIndexPath
you have to set identifier.
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中触摸或单击的tableView单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!