ios 如何检测快速触摸或点击的tableView单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31182847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect tableView cell touched or clicked in swift
提问by pawisoon
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.
我正在尝试获取index
所选项目TableView
并在此之后开始一些活动。不幸的是,我发现的大多数解决方案都在 Objective-c 中或不起作用。
Method func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
don't print the cell
label..
方法func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
不打印cell
标签..
Can somebody help me please?
有人可以帮我吗?
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() } }
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...
几次尝试后,我将代码更改为与我找到的教程不同的代码。它也不起作用。现在我想这是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])!") } }
回答by Ashish Kakkad
If you want the value from cell then you don't have to recreate cell in the didSelectRowAtIndexPath
如果您想要单元格中的值,那么您不必在 didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println(tasks[indexPath.row])
}
Task would be as follows :
任务如下:
let tasks=["Short walk",
"Audiometry",
"Finger tapping",
"Reaction time",
"Spatial span memory"
]
also you have to check the cellForRowAtIndexPath
you have to set identifier.
您还必须检查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
}
Hope it helps.
希望能帮助到你。
回答by jaiswal Rajan
In Swift 3.0
在 Swift 3.0 中
You can find the event for the touch/click of the cell of tableview through it delegate method. As well, can find the section and row value of the cell like this.
您可以通过 it delegate 方法找到 tableview 单元格的触摸/单击事件。同样,可以像这样找到单元格的部分和行值。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}
回答by spencer.sm
A of couple things that need to happen...
一些需要发生的事情......
The view controller needs to extend the type
UITableViewDelegate
The view controller needs to include the
didSelectRowAt
function.The table view must have the view controller assigned as its delegate.
视图控制器需要扩展类型
UITableViewDelegate
视图控制器需要包含该
didSelectRowAt
功能。表视图必须将视图控制器分配为其委托。
Below is one place where assigning the delegate could take place (within the view controller).
下面是一个可以分配委托的地方(在视图控制器中)。
override func loadView() {
tableView.dataSource = self
tableView.delegate = self
view = tableView
}
And a simple implementation of the didSelectRowAt
function.
以及didSelectRowAt
函数的简单实现。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row: \(indexPath.row)")
}
回答by pawisoon
回答by Mr Duck
This worked good for me:
这对我很有用:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}
The output should be:
输出应该是:
section: 0
row: 0
回答by Pankaj Jangid
Inherit the tableview delegate and datasource. Implement delegates what you need.
继承 tableview 委托和数据源。实现您需要的委托。
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
And Finally implement this delegate
最后实现这个委托
func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath) {
print("row selected : \(indexPath.row)")
}
回答by Sanjay Mali
To get an elements from Array in tableView cell touched or clicked in swift
快速触摸或单击 tableView 单元格中的 Array 中的元素
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text= arr_AsianCountries[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexpath = arr_AsianCountries[indexPath.row]
print("indexpath:\(indexpath)")
}
回答by Prabhat Pandey
# Check delegate? first must be connected owner of view controller
# Simple implementation of the didSelectRowAt function.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("row selection: \(indexPath.row)")
}
回答by Lexter
I screw up on the every time! Just make sure the tableView
delegate and dataSource
are declared in viewDidLoad
. Then I normally populate a few arrays to simulate returned data and then take it from there!
我每次都搞砸了!只需确保tableView
委托 和dataSource
在viewDidLoad
. 然后我通常会填充一些数组来模拟返回的数据,然后从那里获取它!
//******** Populate Table with data ***********
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? SetupCellView
cell?.ControllerLbl.text = ViewContHeading[indexPath.row]
cell?.DetailLbl.text = ViewContDetail[indexPath.row]
cell?.StartupImageImg.image = UIImage(named: ViewContImages[indexPath.row])
return cell!
}