如何UIImageView在表格单元格中添加手势识别器?我想要它,这样如果用户点击单元格中的图像,图像将会改变,数据模型将更新.
我知道这需要设置在UITableViewController.我的代码当前可以执行命令,如果单元格中的任何位置被轻击,但我希望它只在图像被轻敲时执行,而不是在单元格中的任何位置执行.
我在中设置了手势识别器 viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Load sample data
loadSampleHabits()
// Initialize tap gesture recognizer
var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:)))
// Add gesture recognizer to the view
self.tableView.addGestureRecognizer(recognizer)
Run Code Online (Sandbox Code Playgroud)
这就是功能
//action method for gesture recognizer
func tapEdit(recognizer: UITapGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.ended {
let tapLocation = recognizer.location(in: self.tableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? HabitTableViewCell {
print("Row Selected")
}
}
}
Run Code Online (Sandbox Code Playgroud)

作为第二个问题,如果我想在单元格中添加手势识别器和单元格中的图像视图,是否存在任何冲突?