如何向UITableViewCell添加手势?

编程入门 行业动态 更新时间:2024-10-07 12:27:22
本文介绍了如何向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添加手势?

本文发布于:2023-11-03 03:58:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:手势   UITableViewCell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!