Fat*_*dle 10 uitableview uigesturerecognizer ios swift
我想为其中的每个单元格添加一个轻击手势,以UITableView编辑其中的内容.添加手势的两种方式是代码或故事板.我试过了两次但他们失败了.
我可以通过故事板拖放为表格中的每个单元格添加手势吗?它似乎只为第一个单元添加手势.在代码中添加手势,我写了类似的东西,
addGestureRecognizer(UITapGestureRecognizer(target: self,action:#selector(MyTableViewCell.tapEdit(_:))))
Run Code Online (Sandbox Code Playgroud)
要么
addGestureRecognizer(UITapGestureRecognizer(target: self, action:"tapEdit:"))
Run Code Online (Sandbox Code Playgroud)
都工作.但我想让UITableViewController这个手势处理,因为它对数据源做了一些事情.我如何写我的目标和行动?
编辑:
addGestureRecognizer(UITapGestureRecognizer(target: MasterTableViewController.self, action:#selector(MasterTableViewController.newTapEdit(_:)))
Run Code Online (Sandbox Code Playgroud)
它引发一个错误说,无法识别的选择器发送到类0x106e674e0 ...
bil*_*p22 39
要向UITableViewCell添加手势,您可以按照以下步骤操作:
首先,将手势识别器添加到UITableView
tapGesture = UITapGestureRecognizer(target: self, action: #selector(tableViewController.tapEdit(_:)))
tableView.addGestureRecognizer(tapGesture!)
tapGesture!.delegate = self
Run Code Online (Sandbox Code Playgroud)
然后,定义选择器.使用recognizer.locationInView找到你的tableView轻按细胞.您可以访问dataSource中的数据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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
可以直接向TableView单元添加手势并访问viewController中的数据源,您需要设置一个委托:
在自定义单元格中:
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()
}
Run Code Online (Sandbox Code Playgroud)
在你的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
}
}
Run Code Online (Sandbox Code Playgroud)
但是,此方法可能会导致问题,请参阅UIGestureRecognizer和UITableViewCell问题.在这种情况下,当滑动手势成功时,由于某种原因选择器被调用两次.我不能说第二种方法是坏的,因为我还没有找到任何直接证据,但在搜索谷歌之后,似乎第一种方法是标准方法.
您无需添加手势识别器即可实现您的目标.
UITableViewDelegate方法tableView:didSelectRowAtIndexPath:检测哪个行被点击(这正是您tapGesture要做的),然后进行所需的处理.tableView:didEndDisplayingCell:forRowAtIndexPath:在返回单元格之前在刚才输入:cell?.selectionStyle = .None| 归档时间: |
|
| 查看次数: |
30053 次 |
| 最近记录: |