gpt4 book ai didi

ios - 如何给 UITableViewCell 添加手势?

转载 作者:IT王子 更新时间:2023-10-29 05:32:25 27 4
gpt4 key购买 nike

我想向 UITableView 中的每个单元格添加一个点击手势,以编辑其中的内容。添加手势的两种方法是在代码中或通过 Storyboard。我都试过了,但都失败了。

我可以通过 Storyboard拖放向表格中的每个单元格添加手势吗?它似乎只向第一个单元格添加手势。在代码中添加手势,我写了类似的东西,

addGestureRecognizer(UITapGestureRecognizer(target: self,action:#selector(MyTableViewCell.tapEdit(_:))))

addGestureRecognizer(UITapGestureRecognizer(target: self, action:"tapEdit:"))

两者都有效。但我想让 UITableViewController 处理这个手势,因为它对数据源做了一些事情。如何编写我的目标和操作?

编辑:

addGestureRecognizer(UITapGestureRecognizer(target: MasterTableViewController.self, action:#selector(MasterTableViewController.newTapEdit(_:)))

它引发一个错误说,无法识别的选择器发送到类 0x106e674e0...

最佳答案

为UITableViewCell添加手势,可以按照以下步骤进行:

首先,给UITableView添加手势识别器

tapGesture = UITapGestureRecognizer(target: self, action: #selector(tableViewController.tapEdit(_:)))
tableView.addGestureRecognizer(tapGesture!)
tapGesture!.delegate = self

然后,定义选择器。使用 recognizer.locationInView 定位您在 tableView 中点击的单元格。您可以通过 tapIndexPath 访问数据源中的数据,这是用户点击的单元格的 indexPath。

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 中访问数据源,您需要设置一个委托(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 and UITableViewCell issue .在这种情况下,当滑动手势成功时,选择器由于某种原因被调用两次。我不能说第二种方法不好,因为我还没有找到任何直接证据,但通过谷歌搜索后,似乎第一种方法是标准方法。

关于ios - 如何给 UITableViewCell 添加手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37692978/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com