检测UITableViewCell上的双击和单击以执行两个操作 [英] Detecting double and single tap on UITableViewCell to perform two action

查看:105
本文介绍了检测UITableViewCell上的双击和单击以执行两个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PFQueryTableViewController ,其中我正在尝试学习一些tableView交互。

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

我现在拥有的东西:tableView单元格在点击 didSelectRowAtIndexPath 时增加其高度,或者我基本上可以使细胞相关数据到下一个viewController使用 performSegueWithIdentifier prepareForSegue 一次点击。

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.

在下面的代码中,如果我评论点击代码以增加tableView单元格的高度代码,我可以检测到双击。其他方面,单击会增加高度,因为代码在didSelect块内。

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.

我需要什么:
单击时,单元格高度增加或减少。当单元格高度增加时,如果我双击,它应该将单元格数据分割到下一个 UIViewController

如果没有,单击会增加或减少高度。并且双击应该将单元格数据划分到下一个 UIViewController

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

代码如下:

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

我可以让它工作的另一种方法是,如果我能得到 NSIndexPath在 didSelectRowAtIndexPath 代码之外的所选单元格的,我基本上可以使用双击执行if else来获取所需的操作。你能在tableView默认块之外得到 NSIndexPath 吗?

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?

推荐答案

搞定了工作!

首先:定义var customNSIndexPath = NSIndexPath()并在 didSelectRowAtIndexPath 代码块中添加以下代码。

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

customNSIndexPath = indexPath
        print(customNSIndexPath)

第二步:从 didSelectRowAtIndexPath 中删除​​点击代码以增加tableView单元格的高度代码并添加到 singleTap()功能。并使用 doubleTap()执行segue。

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

注意:如果有人有更好的解决方案,或者某些xyz案例中的错误,请发表评论。如果你的更好,它会真正帮助别人。

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.

这篇关于检测UITableViewCell上的双击和单击以执行两个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆