检测UITableViewCell上的双击和单击以执行两个操作

NS1*_*518 5 uitableview ios uitapgesturerecognizer swift uitableviewautomaticdimension

我有一个PFQueryTableViewController我正在尝试学习一些tableView交互.

我目前拥有的:tableView单元格在水龙头上增加其高度,didSelectRowAtIndexPath或者我基本上可以将单元格相关的数据performSegueWithIdentifierprepareForSegue单个水龙头一起使用.

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

我需要的:在单击时,细胞高度增加或减少.当细胞高度增加时,如果我双击,它应该将细胞数据分割到下一个UIViewController.

如果没有,单击应增加或减少高度.并且双击应该将细胞数据分割到下一个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")
    }
Run Code Online (Sandbox Code Playgroud)

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

NS1*_*518 3

成功了!

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

customNSIndexPath = indexPath
        print(customNSIndexPath)
Run Code Online (Sandbox Code Playgroud)

didSelectRowAtIndexPath第二:从函数中删除并添加“点击代码以增加 tableView 单元格的高度”代码singleTap()。并使用 执行 segue 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()
    }
Run Code Online (Sandbox Code Playgroud)

这就是你工作到很晚而不睡觉的原因。这是最容易做的事情。谢谢

注意:如果有人有更好的解决方案,或者这个解决方案在某些 xyz 情况下是错误的,请发表评论。如果你的更好,那真的会帮助其他人。