如何向UITableViewCell添加手势? [英] How to add gesture to UITableViewCell?
问题描述
我想向UITableView
中的每个单元格添加轻击手势,以编辑其中的内容.添加手势的两种方法是在代码中或通过情节提要.我都尝试过,但都失败了.
I want to add a tap gesture to every cell in a UITableView
that edits the content in it. The two ways to add a gesture are in code or through storyboard. I tried both and they failed.
我可以通过拖放情节提要向表中的每个单元格添加手势吗?似乎只向第一个单元格添加了手势.在代码中添加手势,我写了类似的东西,
Can I add a gesture to every cell in table with storyboard drag and drop? It seems to only add gesture to the first cell. Adding gesture in code, I wrote something like,
addGestureRecognizer(UITapGestureRecognizer(target: self,action:#selector(MyTableViewCell.tapEdit(_:))))
或
addGestureRecognizer(UITapGestureRecognizer(target: self, action:"tapEdit:"))
都工作.但是我想让UITableViewController
处理这个手势,因为它对数据源有作用.我该如何写我的目标和行动?
both work. But I'd like to let the UITableViewController
handle this gesture because it does something with the datasource. How do I write my target and action?
addGestureRecognizer(UITapGestureRecognizer(target: MasterTableViewController.self, action:#selector(MasterTableViewController.newTapEdit(_:)))
它会引发错误,表示无法识别的选择器已发送给类0x106e674e0 ...
it induce an error said, unrecognized selector sent to class 0x106e674e0...
推荐答案
要将手势添加到UITableViewCell,可以按照以下步骤操作:
To add gesture to UITableViewCell, you can follow the steps below:
首先,将手势识别器添加到UITableView
First, add gesture recognizer to UITableView
tapGesture = UITapGestureRecognizer(target: self, action: #selector(tableViewController.tapEdit(_:)))
tableView.addGestureRecognizer(tapGesture!)
tapGesture!.delegate = self
然后,定义选择器.使用recognizer.locationInView
定位您在tableView中点击的单元格.您可以通过tapIndexPath
访问数据源中的数据,该数据是用户点击的单元格的indexPath.
Then, define the selector. Use recognizer.locationInView
to locate the cell you tap in tableView. And you can access the data in your dataSource by tapIndexPath
, which is the indexPath of the cell the user tapped.
func tapEdit(recognizer: UITapGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.Ended {
let tapLocation = recognizer.locationInView(self.tableView)
if let tapIndexPath = self.tableView.indexPathForRowAtPoint(tapLocation) {
if let tappedCell = self.tableView.cellForRowAtIndexPath(tapIndexPath) as? MyTableViewCell {
//do what you want to cell here
}
}
}
}
可以将手势直接添加到TableView单元并访问viewController中的数据源,您需要设置一个委托:
It is possible to add gesture directly to TableView cell and access the datasource in viewController, You need to set up a delegate:
在您的自定义单元格中:
import UIKit
class MyTableViewCell: UITableViewCell {
var delegate: myTableDelegate?
override func awakeFromNib() {
super.awakeFromNib()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(MyTableViewCell.tapEdit(_:)))
addGestureRecognizer(tapGesture)
//tapGesture.delegate = ViewController()
}
func tapEdit(sender: UITapGestureRecognizer) {
delegate?.myTableDelegate()
}
}
protocol myTableDelegate {
func myTableDelegate()
}
在您的viewController中:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate, myTableDelegate {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 35
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? MyTableViewCell
cell?.delegate = self
return cell!
}
func myTableDelegate() {
print("tapped")
//modify your datasource here
}
}
但是,此方法可能会导致问题,请参见 UIGestureRecognizer和UITableViewCell问题.在这种情况下,当滑动手势成功时,选择器由于某种原因会被调用两次.我不能说第二种方法不好,因为我还没有找到任何直接的证据,但是在通过Google搜索之后,第一种方法似乎是标准方法.
However, this method could cause problems, see UIGestureRecognizer and UITableViewCell issue. In this case, when the swipe gesture successes, the selector get called twice for some reason. I can't say the second method is a bad one as I haven't found any direct evidence yet, but after searching through Google, it seems like the first method is the standard way.
这篇关于如何向UITableViewCell添加手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!