Double and single click detection on UITableViewCell to perform two actions

I have a PFQueryTableViewController in which I am trying to learn some tableView interactions.

I currently have: a tableView cell increases its height on the didSelectRowAtIndexPath , or I can basically list the cell related data to the next viewController using performSegueWithIdentifier along with prepareForSegue with one click.

In the code below, if I comment out the code "Tap code to height height tableView cell", I can detect a double tap. Another wise single tap increases the height as the code inside is done. Select a block.

What I need: With a single press, the cell height increases or decreases. While the cell height increases, if I double-click it, it should pass the cell data to the next UIViewController .

If not, a single crane should increase or decrease the height. And the double tap should pass the cell data to the next UIViewController

The code is as follows:

 var selectedCellIndexPath: NSIndexPath? var SelectedCellHeight = CGFloat() // currently set to 480.0 tableView height var UnselectedCellHeight = CGFloat() // currently set to 300.0 tableView unselected hight .... override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if let selectedCellIndexPath = selectedCellIndexPath { if selectedCellIndexPath == indexPath { return SelectedCellHeight } } return UnselectedCellHeight } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell! if cell == nil { cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") } // Below is the double Tap gesture recognizer code in which // The single tap is working, double tap is quite difficult to get // as the cell height increases on single tap of the cell let doubleTap = UITapGestureRecognizer() doubleTap.numberOfTapsRequired = 2 doubleTap.numberOfTouchesRequired = 1 tableView.addGestureRecognizer(doubleTap) let singleTap = UITapGestureRecognizer() singleTap.numberOfTapsRequired = 1 singleTap.numberOfTouchesRequired = 1 singleTap.requireGestureRecognizerToFail(doubleTap) tableView.addGestureRecognizer(singleTap) // Tap code to increases height of tableView cell if let selectedCellIndexPath = selectedCellIndexPath { if selectedCellIndexPath == indexPath { self.selectedCellIndexPath = nil cell.postComment.fadeIn() cell.postCreatorPicture.alpha = 1 cell.postDate.fadeIn() // cell.postTime.fadeIn() cell.postCreator.fadeIn() } else { self.selectedCellIndexPath = indexPath } } else { selectedCellIndexPath = indexPath } tableView.beginUpdates() tableView.endUpdates() } func doubleTap() { print("Double Tap") } func singleTap() { print("Single Tap") } 

In another way, I can make it work, if I can get the NSIndexPath selected cell outside the didSelectRowAtIndexPath code, I can basically double-click to execute, if necessary, to get the required action. Can you get NSIndexPath outside tables by default tableView?

+5
source share
1 answer

It worked!

First: Define var customNSIndexPath = NSIndexPath () and add the code below inside the didSelectRowAtIndexPath code didSelectRowAtIndexPath .

 customNSIndexPath = indexPath print(customNSIndexPath) 

Second: remove the code β€œTouch the code to increase the height of the table tableView” from didSelectRowAtIndexPath and add the singleTap() function. And do segue using doubleTap() .

 func doubleTap() { print("Double Tap") performSegueWithIdentifier("MainToPost", sender: self) } func singleTap() { print("Single Tap") var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell! if cell == nil { cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") } if let selectedCellIndexPath = selectedCellIndexPath { if selectedCellIndexPath == customNSIndexPath { self.selectedCellIndexPath = nil } else { self.selectedCellIndexPath = customNSIndexPath } } else { selectedCellIndexPath = customNSIndexPath } tableView.beginUpdates() tableView.endUpdates() } 

This is how you start working late, do not sleep. It was the simplest thing. thanks

Note. If someone has a better solution, or it is wrong in some cases xyz, make a comment. If yours is better, it will really help others.

+3
source

Source: https://habr.com/ru/post/1240261/


All Articles