ios Swift 尝试将表格视图单元格上的点击事件链接到控制器方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25818450/
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
Swift Trying to link a tap event on a table view cell to a controller method
提问by WebQube
I am new to swift and havne't programmed in objective C. So i'm new :-) trying to do something fairly simple. Link a table view cell click to call a method in the controller like so http://prntscr.com/4m9kf7
我是 swift 的新手,还没有在目标 C 中编程。所以我是新手:-) 试图做一些相当简单的事情。链接表视图单元格单击以调用控制器中的方法,例如 http://prntscr.com/4m9kf7
Edit:
编辑:
i have these methods in my MasterController
我的 MasterController 中有这些方法
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
I have added breakpoints in all 4 of them. and when clicking on the cell it doesn't stop in either of them. See Screen shot:
我在所有 4 个中都添加了断点。当单击单元格时,它不会在其中任何一个中停止。请参阅屏幕截图:
回答by Woodstock
You need to implement the didSelectRowAtIndexPath
delegate method and put your handling code inside it.
您需要实现didSelectRowAtIndexPath
委托方法并将处理代码放入其中。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
}
回答by Alex Zanfir
For Swift 4:
对于 Swift 4:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row selected: \(indexPath.row)")
}