检测UITableViewCell上的双击和单击执行两个操作
我有一个PFQueryTableViewController
,我正在试图学习一些tableView交互。
我目前有什么:tableView单元格增加其高度didSelectRowAtIndexPath
或我基本上可以segue单元相关的数据到下一个viewController使用performSegueWithIdentifier
与prepareForSegue
一起使用一个水龙头。
在下面的代码中,如果我注释“点击代码来增加tableView单元格的高度”代码,我可以检测到双击。 其他方面,由于代码位于didSelect块内,单击可以增加高度。
我需要的是:单击,单元格高度增加或减less。 当单元格高度增加时,如果我双击,它应该将单元格数据移到下一个UIViewController
。
如果不是这样,那么单一的水龙头应该增加或减less高度。 并且双击应该将细胞数据传递给下一个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") }
另一种方式,我可以使它的工作是,如果我可以获得选定的单元格的didSelectRowAtIndexPath
代码以外的NSIndexPath
,我基本上可以使用双击执行,否则获取所需的操作。 你可以得到NSIndexPath
外的tableView默认块?
得到它的工作!
首先:定义var customNSIndexPath = NSIndexPath()并在didSelectRowAtIndexPath
代码块中添加下面的代码。
customNSIndexPath = indexPath print(customNSIndexPath)
第二:从didSelectRowAtIndexPath
删除“点击代码以增加tableView单元的高度”代码并添加到singleTap()
函数中。 并使用doubleTap()
执行segue。
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() }
这就是你没有睡觉就迟到了。 这是最容易做的事情。 谢谢
注意:如果有人有更好的解决方法或这个xyz的情况是错误的,请做评论。 如果你的更好,这真的会帮助别人。