I have several tap gestures being created and recognized on different UIViews from in my TableViewController and the different gestures are being recognized correctly. As seen in this code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let tapOnView1 = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
let tapOnView2 = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
cell.View1.addGestureRecognizer(tapOnView1)
cell.View1.userInteractionEnabled = true
cell.View2.addGestureRecognizer(tapOnView1)
cell.View2.userInteractionEnabled = true
return cell
}
My Handle tap looks like this:
func handleTap(sender:UITapGestureRecognizer) {
let tappedView = sender.view
self.tableView.beginUpdates()
if tappedView == cell.View1 {
print("View 1 Tapped")
} else if tappedView == cell.View2 {
print("View 2 Tapped")
}
}
I wanted to move all this code to my CustomCell UITableViewCell class as there are actually several more UIViews that have different actions that need to be taken on a tap. Additionally moving them all to the Cell itself seems to me to be the right thing to do. I searched for answers but the only true answer I have seen is to use buttons and there are several reasons that this is really not an option for me without some serious refactoring and rewriting. I have tried several iterations to the following code in my CustomCell class:
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let tapOnView1 = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
addGestureRecognizer(tapOnView1)
let tapOnView2 = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
addGestureRecognizer(tapOnView2)
}
and this handleTap function:
func handleTap(sender: UITapGestureRecognizer) {
if delegate != nil && item != nil {
if sender.view == view1 {
print("view1 tapped")
} else {
print("view2 tapped")
}
}
}
The tap for view1 is never called. It only ever calls the view2 tap no matter where in the cell I tap. I have tried using different Selector functions (i.e. handleTapOne: for View1, and handleTapTwo: for View2) but I can't seem to figure out how to do this.
Again it works in my UITableViewController but it does not work when I try to move all of the tap recognizers to the UITableViewCell.
Thanks for the help.
iphonic answered my question above. I was an idiot I guess. staring at a problem for two long and missing the most simplest of things.
Related
I have a tableView with cells. Each cell has an imageView. When I click on the imageView, I want to execute a function with parameters that I pass in.
The only way that I know how to work with clicking on imageViews is something like this:
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(printNumber))
cell.imageView.addGestureRecognizer(gestureRecognizer)
#objc func printNumber(){
print("4")
}
Now, imagine this exact same thing, but I want to pass the number to print into the function
I've seen a million different posts about how you can't pass parameters into selectors, so I'm not sure what to do in this case.
I want to do something like this (I know you can't do this)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(printNumber(num: 4))
cell.imageView.addGestureRecognizer(gestureRecognizer)
#objc func printNumber(String: num){
print(num)
}
I need to do this because every button will have a different output when pressed, depending on some other variables in the cell.
You don't need to pass in the number. Assuming the recognizer is on the image view you:
Get the gestures.view (this is the image view)
Walk up the responder chain to find the parent tableview cell and table view
Ask the tableview for the index path of the tableview cell (this is basically he number you wanted to pass in)
Call your function with the number
As for walking up the responder chain generically:
extension UIResponder {
func firstParent<Responder: UIResponder>(ofType type: Responder.Type ) -> Responder? {
next as? Responder ?? next.flatMap { $0.firstParent(ofType: type) }
}
}
so your code is:
guard let cell = recognizer.view?.firstParent(ofType: UITableViewCell.self),
let tableView = recognizer.view?.firstParent(ofType: UITableView.self),
let indexPath = tableview.indexPath(for: cell) else {
return
}
// Do stuff with indexPath here
Use the accessibilityIdentifier property, only if you still want to use the #selector, so that UIGestureRecognizer can read the value of the value object.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourTableViewCell
…
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(printNumber))
cell.imageView.accessibilityValue = String(4)
cell. imageView.isUserInteractionEnabled = true
cell.imageView.addGestureRecognizer(gestureRecognizer)
…
return cell
}
#objc func printNumber(sender: UITapGestureRecognizer) {
if let id = sender.view?.accessibilityIdentifier {
print(tag)
}
}
In my Swift code, I have a UICollectionViewCell with 3 buttons (all three have IBActions). From my UICollectionViewController I now want to "catch" the individual button taps.
I've followed this StackOverflow question and I can catch the UICollectionViewCell's touch-up inside up in my CollectionViewController with adding this line to the viewDidLoad
gestureRecognizer.cancelsTouchesInView = false
and with this function
func handleTapForCell(recognizer: UITapGestureRecognizer){
//I can break in here
}
But the piece missing now is how can I figure out which of the three buttons have been tapped? I have set different tags on the buttons but I have not found any place on the gestureRecognizer dealing with these tags.
Any ideas?
I think, you don't need to add Gesture on cell to get a button action of a tableviewCell. This code may help you:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Your tableviewCell code here
//set tag of cell button
cell.button1.tag = 1
cell.button2.tag = 2
cell.button3.tag = 3
//add action of your cell button
cell.button1.addTarget(self, action: Selector("cellButtonTapped:event:"), forControlEvents: .TouchUpInside)
cell.button2.addTarget(self, action: Selector("cellButtonTapped:event:"), forControlEvents: .TouchUpInside)
cell.button3.addTarget(self, action: Selector("cellButtonTapped:event:"), forControlEvents: .TouchUpInside)
// return cell
}
func cellButtonTapped(sender:UIButton, event:AnyObject){
let touches: NSSet = event.allTouches()!
let touch = touches.anyObject()
let currentTouchPosition: CGPoint = (touch?.locationInView(YOUR_TABLEVIEW_INSTANCE))!
if let indexPath: NSIndexPath = self.YOUR_TABLEVIEW_INSTANCE.indexPathForRowAtPoint(currentTouchPosition)!{
if sender.tag == 1{
//cell first button tap
}else sender.tag == 2{
//cell second button tap
}
else sender.tag == 3{
//cell 3rd button tap
}
}
}
You can follow the protocol/delegate paradigm.
What you need to do is define a protocol in Custom cell. Then make the viewcontroller subscribe to the cell delegate.
Implement the IBActions inside the custom cell class. Call the delegate methods in the IBActions of the buttons. viewcontroller who is delegating for the cell will receive the callbacks for button taps inside the cell.
I have an UICollectionView and I want to add pan gesture to its Cells/Items. When I add the gesture in usual way UICollectionView is not getting scrolled.
This is how I add the gesture
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(CaptureViewController.pagesCollectionViewItemPanEvent(_:)))
cell.addGestureRecognizer(panGesture)
return cell;
}
Is there something wrong here? Can someone please tell me a way to get my work done. Any help would be highly appreciated.
You should add the gesture to the collection view and not to the cell itself.
Something like...
let panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
collectionView.addGestureRecognizer(panGesture)
func handlePanGesture(gesture: UIPanGestureRecognizer) {
let locationInView = gesture.locationInView(collectionView)
...
}
Just a proposal, I didn't test it:
Create a custom cell:
class PanCell: UICollectionViewCell {
override func awakeFromNib() {
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.pagesCollectionViewItemPanEvent(_:)))
self.addGestureRecognizer(panGesture)
}
}
and you can use the delegation to inform CaptureViewController.
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...
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.
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
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")
}
}
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
}
Using iOS 9 and facing a problem with a UITapGestureRecognizer. I have a ViewController-A with a UITableView. I have added a tableViewCell which has a textLabel. I want to implement tap on the textLabel. So if I tap on textLabel -- it should print on Console or do anything else
Issue: TapRecogniser is not working. Getting the below error:
Following is what I have done:
1) Added a `UITapGestureRecognizer' on the textLabel (From StoryBoard). Enabled User Interaction for the textLabel (the error even now)
2) Following is the IBAction:
#IBAction func nameTap(sender: UITapGestureRecognizer) {
print("a")
}
3) CellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ThirdViewCell!
cell.nameLabel?.text = "XYZ"
let nameTapRecognizer = UITapGestureRecognizer(target: self, action: Selector("nameTap:"))
nameTapRecognizer.cancelsTouchesInView = false
cell.nameLabel?.addGestureRecognizer(nameTapRecognizer)
return cell
}
P.S:
1) This was working in iOS 8. I have checked..There are no duplicates (there is only one tap recognizer in the entire file and its linked to the textLabel)
2) I don't want to use didSelectRowAtIndexPath method as I need to implement TapGestureRecognizer for more textLabels within the tableViewCell.
are you see the error console Label, and the property as UserInteractionEnabled = NO; see the screen shot
try this
let nameTapRecognizer = UITapGestureRecognizer(target: self, action: Selector("nameTap:"))
nameTapRecognizer.cancelsTouchesInView = false
cell.nameLabel?.tag = indexPath.row // add this
nameTapRecognizer.numberOfTapsRequired = 1 // add this
nameTapRecognizer.delegate =self
cell.nameLabel?.userInteractionEnabled = true // add this
cell.nameLabel?.addGestureRecognizer(nameTapRecognizer)
// method
func nameTap(gesture: UITapGestureRecognizer) {
let indexPath = NSIndexPath(forRow: gesture.view!.tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
// Do whatever you want.
}