如何检测tableView单元格被触摸或快速点击
我试图在TableView获取所选项目的index ,然后开始一些活动。 不幸的是,我发现的大部分解决方案都在objective-c中,或者不起作用。
方法func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)不打印cell标签..
有人可以帮我吗?
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模拟器的问题...
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重新创建单元格
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中
您可以通过它的委托方法找到触摸/点击tableview单元格的事件。 同样,可以像这样找到单元格的段和行值。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: (indexPath.section)")
print("row: (indexPath.row)")
}
我自己使用weheartswift教程解决了问题

上一篇: How to detect tableView cell touched or clicked in swift
