[英]Adding a Tap Gesture Recognizer to TableViewCell.swift
我有一个表格视图单元格,其中带有一个图像,该图像已附加了Tap Gesture Recognizer。
表视图中原型TableViewCell的图像,带有两个标签和一个图像。 顶部是Tap Gesture Recognizer徽标:
当我尝试ctrl并将Tap Gesture Recognizer拖动到TableViewCellController
, TableViewCell
文件或任何其他文件时,它不会让我使用。
但是,在不是表视图的其他视图上,我可以使其正常运行。 我是Swift的新手。 有谁知道这是为什么发生的,以及我可以做些什么来帮助它(我知道我可以改用按钮,但是我从中学不到任何东西)?
TableViewCell
和TableViewCellController
文件都有其默认代码。
编辑 :我想要的是用户能够点击图像以将+1添加到表单元格中的类的Int属性。 类和单元中的UILabel之一已更新。 我可以在常规视图中毫无问题地实现这种行为,并且希望在表视图中实现。
对于UITableViewCell
您无法通过情节UITapgesture
向其添加UITapgesture
,因为它只是一个原型单元,与其他单元格不同。 如果您确实想这样做,可以在委托中以编程方式进行,如下所示:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
let tapGesture = UITapGestureRecognizer(target: self, action: "SomeMethod")
cell.myImageView.addGestureRecognizer(tapGesture)
return cell
}
但是我找不到你为什么要这么做。 如果您只想在点击单元格时执行某项操作或调用某些函数,则应使用didSelectRowAtIndexPath
。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("cell at #\(indexPath.row) is selected!")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.