我尝试在带有自定义单元格的自定义 tableView 中的多个元素上使用gestureRecognizer。
我有两个链接到特定操作的 imageView。 如果我不触摸这些图像,我想为单元格的背景着色。
因此代码一定不能位于 didSelectRowAtIndexPath 方法中。
我尝试了类似的方法,但对于该程序来说,图像似乎从未被触摸过。
我哪里理解错了?
class MyTableExampleViewController: UIViewController, UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Gesture recognizer
let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
self.tableView.addGestureRecognizer(tapGestureRecognizer)
}
...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:CustomExampleCell = tableView.dequeueReusableCellWithIdentifier("CustomExampleCell") as! CustomExampleCell
cell.playImageView.image = UIImage(named: play)
cell.stopImageView.image = UIImage(named: stop)
return cell
}
func handleTapGestureRecognizer(gestureRecognizer:UITapGestureRecognizer) {
let touchPoint:CGPoint = gestureRecognizer.locationInView(self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(touchPoint)
let cell:CustomExampleCell = tableView.dequeueReusableCellWithIdentifier("CustomExampleCell") as! CustomExampleCell
if let _ = cell.playImageView.layer.hitTest(touchPoint) {
print("Action Play")
} else if let _ = cell.stopImageView.layer.hitTest(touchPoint) {
print("Action Stop")
} else {
cell.backgroundColor = UIColor.greenColor()
}
}
}
最佳答案
您需要处理 CustomExampleCell 中的触摸。然后实现一个委托(delegate)来通知您的 View Controller (如果您需要)。
关于swift - UITableView 在 SWIFT 的多个 View 上带有手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600594/