14

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(_:))))

or

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

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?

EDIT:

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

it induce an error said, unrecognized selector sent to class 0x106e674e0...

Forge
  • 6,538
  • 6
  • 44
  • 64
Fate Riddle
  • 364
  • 1
  • 3
  • 15
  • Add UIButton on UITableViewCell with a unique tag number of each button. Assign a one method to button. You can get tag number on that method – kb920 Jun 08 '16 at 04:17

4 Answers4

48

To add gesture to UITableViewCell, you can follow the steps below:

First, add gesture recognizer to UITableView

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

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

            }
        }
    }
}

It is possible to add gesture directly to TableView cell and access the datasource in viewController, You need to set up a delegate:

In your custom cell:

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() 
}

In your 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
    }

}

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.

Community
  • 1
  • 1
ilovecomputer
  • 4,238
  • 1
  • 20
  • 33
  • It seems natural to add gesture to tableViewCell rather than tableView then specify which in code. And for more complicated gestures like pan, writing condition checks if a pan starts/stays/ends in a cell and handle other cases can be painful for me. I'd like to add gesture to tableviewcell, writing code in tableviewcell.swift. is it possible? And can you answer my second question as how do I write the target & selector of UITapGestureRecognizer(target, action) – Fate Riddle Jun 08 '16 at 04:35
  • Target & selector `let tapGesture = UITapGestureRecognizer(target: self, action: #selector(MyTableViewCell.tapEdit(_:)))` – ilovecomputer Jun 08 '16 at 05:50
  • let tapGesture = UITapGestureRecognizer(target: self, action: #selector(MyTableViewCell.tapEdit(_:))) is working. But in my case, the gesture interacts with datasource, so I'd like the target be UITableViewController, and uses one of the function in tere(which can alter datasource), How to do that? – Fate Riddle Jun 08 '16 at 05:55
  • Thanks, it works. Out of curiosity, target can only be self (or something belongs to the class)? – Fate Riddle Jun 08 '16 at 08:56
  • @FateRiddle No. target is an object that is a recipient of action messages sent by the receiver when the represented gesture occurs. You may call this method multiple times to specify multiple target-action pairs. However, if you request to add a target-action pair that has already been added, then the request is ignored. “self” here is your custom cell – ilovecomputer Jun 08 '16 at 09:20
  • It seems I cannot call a `UITableViewController` as target if the code `UITapGestureRecognizer(target: MasterViewController ...)` is in `MyUITableViewCell.swift`. Error occurs. @luiyezheng – Fate Riddle Jun 16 '16 at 03:53
  • Please could someone tell me how to identify which cell was clicked in this method : func myTableDelegate() { print("tapped") } – Mamta Jan 07 '17 at 10:43
11

You don't need to add gesture recognizer to achieve what you are doing.

  • Use the UITableViewDelegate method tableView:didSelectRowAtIndexPath: to detect which row is tapped (this is what exactly your tapGesture is going to do) and then do your desired processing.
  • If you don't like the gray indication when you select cell, type this in your tableView:didEndDisplayingCell:forRowAtIndexPath: just before returning the cell:
    cell?.selectionStyle = .None
Rohan Sanap
  • 2,773
  • 2
  • 21
  • 39
4

Adding gesture in awakeFromNib method seems much more easier and works fine.

class TestCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

        let panGesture = UIPanGestureRecognizer(target: self,
                                            action: #selector(gestureAction))
        addGestureRecognizer(panGesture)
    }

    @objc func gestureAction() {
        print("gesture action")
    }
}
LembergSun
  • 641
  • 7
  • 14
0

The easiest way to do this is to add the gesture in a custom UITableViewCell. An easier alternative to setting up a custom delegate pattern is to inform the view controller of the edits would be to use a handler in the form of a closure that the view controller can provide and which is called when user editing is finished. I'm assuming a textField is used to allow cell editing.

class CustomTableViewCell: UITableViewCell {

    func activateTitleEditing() {
        textField.isEnabled = true
        textField.becomeFirstResponder()
    }

    // This will hold the handler closure which the view controller provides
    var resignationHandler: (() -> Void)?

    @objc private func tap(_ recognizer: UITapGestureRecognizer) {
        guard recognizer.state == .ended else { return }
        activateTitleEditing()
    }

    @IBOutlet weak var textField: UITextField! { didSet {
        textField.delegate = self
        let tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
        addGestureRecognizer(tap)
        textField.isEnabled = false
        }}
}

extension CustomTableViewCell: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        resignationHandler?()
    }
}

And within your custom UITableViewController, pass in the handler to be able to make changes to your model. Don't forget to account for possible memory cycles in the closure.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // initialize and return table view cell
    let cell = tableView.dequeueReusableCell(withIdentifier: K.documentCellIdentifier, for: indexPath)
    assert(cell is CustomTableViewCell, "Document cell dequeuing error")
    let customCell = cell as! DocumentTableViewCell
    customCell.textField.text = documentModel.documents[indexPath.row]
    customCell.resignationHandler = { [weak self, unowned customCell] in
        guard let self = self else { return }
        if let newTitle = customCell.textField.text {
            self.cellModel.cells[indexPath.row] = newTitle
        }
    }
    return customCell
}
skmalm
  • 1
  • 2