Detecting double and single tap on UITableViewCell to perform two action

一个人想着一个人 提交于 2019-12-12 09:39:24

问题


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

What I currently have: The tableView cell increases its height on a tap didSelectRowAtIndexPath or I can basically segue cell related data to next viewController using performSegueWithIdentifier together with prepareForSegue using a single tap.

In the below code, if I comment the "Tap code to increases height of tableView cell" code, I can detect double tap. Other wise the single tap increases the height as the code is inside didSelect block.

What I need: On Single tap, cell height increases or decrease. While the cell height is increased, if I double tap, it should segue cell data to next UIViewController.

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

Code is as below:

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

The other way I can make it work is, if I can get NSIndexPath of selected cell outside of didSelectRowAtIndexPath code, I can basically use the double tap to perform if else to get the required action. Can you get NSIndexPath outside the tableView default blocks?


回答1:


Got it working!

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

customNSIndexPath = indexPath
        print(customNSIndexPath)

Second: Remove "Tap code to increases height of tableView cell" code from didSelectRowAtIndexPath and add to the singleTap() function. And perform segue using the 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 screw up working late without sleeping. It was the easiest thing to do. Thanks

Note: If someone has better solution or this one is wrong in some xyz case, please do comment. If yours is better it would really help others.



来源:https://stackoverflow.com/questions/34702700/detecting-double-and-single-tap-on-uitableviewcell-to-perform-two-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!