2016-06-08 152 views
2

我想要在编辑其中的内容的UITableView中的每个单元格中添加点按手势。添加手势的两种方式是使用代码或通过故事板。我试了两次,但都失败了。如何向UITableViewCell添加手势?

我可以添加一个手势每个单元格与故事板拖放?它似乎只为第一个单元添加手势。在代码中添加的姿态,我写的东西一样,

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类...

+0

使用每个按钮的唯一标签号在UITableViewCell上添加UIButton。指定一个方法到按钮。您可以在该方法上获得标签号码 – kb920

回答

17

要添加姿态的UITableViewCell,你可以按照下面的步骤:

第一,向UITableView添加手势识别器

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

然后,define选择器。使用recognizer.locationInView来定位您在tableView中点击的单元格。你可以通过tapIndexPath访问dataSource中的数据,这是用户点击的单元格的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数据源,您需要设置代理:

在您的自定义单元格:

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。在这种情况下,当滑动手势成功时,选择器因某种原因被调用两次。我不能说第二种方法是不好的,因为我还没有找到任何直接的证据,但在搜索Google之后,似乎第一种方法是标准方法。

+0

向tableViewCell添加手势而不是tableView然后指定代码中的哪一个似乎很自然。而对于更复杂的手势(如平移),写入条件会检查平底锅是否开始/停留/结束于单元格中,并处理其他情况对我来说可能会很痛苦。我想给tableviewcell添加手势,在tableviewcell.swift中编写代码。可能吗?你可以回答我的第二个问题,我该如何编写UITapGestureRecognizer的目标和选择器(目标,动作) –

+0

请看我的更新回答 – luiyezheng

+0

目标和选择器'let tapGesture = UITapGestureRecognizer(target:self,action:#selector(MyTableViewCell。 tapEdit(_ :)))' – luiyezheng

4

你不需要添加手势识别器来实现你正在做的事情。

  • 使用UITableViewDelegate方法tableView:didSelectRowAtIndexPath:检测哪一行被窃听(这到底是什么你tapGesture会做),然后做你所需的处理。恢复单元前
    cell?.selectionStyle = .None
+0

点击只是一个例子,我很想做各种手势在每个细胞上。 –

+0

你有没有自定义的单元格类? –

+0

如果是,在自定义单元类 –

-1

在的cellForRowAtIndexPath,:

  • 如果当您选择单元格不喜欢灰色的指示,在您的tableView:didEndDisplayingCell:forRowAtIndexPath:刚刚返回细胞先键入此。创建点按手势并设置为单元格。