Ios 检测UtableViewCell上的双击和单击以执行两个操作
我有一个Ios 检测UtableViewCell上的双击和单击以执行两个操作,ios,swift,uitableview,uitapgesturerecognizer,uitableviewautomaticdimension,Ios,Swift,Uitableview,Uitapgesturerecognizer,Uitableviewautomaticdimension,我有一个PFQueryTableViewController,我试图学习一些tableView交互 我目前拥有的功能:点击didSelectRowAtIndexPath可以增加tableView单元格的高度,或者我基本上可以使用performsguewithidentifier和使用单个点击的prepareforsgue将单元格相关数据分割到下一个viewController 在下面的代码中,如果我对“点击代码以增加tableView单元格的高度”代码进行注释,我可以检测到双击。另一方面,当代码
PFQueryTableViewController
,我试图学习一些tableView交互
我目前拥有的功能:点击didSelectRowAtIndexPath
可以增加tableView单元格的高度,或者我基本上可以使用performsguewithidentifier
和使用单个点击的prepareforsgue
将单元格相关数据分割到下一个viewController
在下面的代码中,如果我对“点击代码以增加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")
}
另一种方法是,如果我可以在didselectrowatinexpath
code之外获取所选单元格的nsindepath
,我基本上可以使用双击来执行if-else以获取所需操作。您能否在tableView默认块之外获得nsindepath
首先:定义var customnsindepath=nsindepath(),并在didselectrowatinexpath
code块中添加以下代码
customNSIndexPath = indexPath
print(customNSIndexPath)
第二:从didSelectRowAtIndexPath
中删除“点击代码以增加tableView单元格的高度”代码,并添加到singleTap()
函数中。并使用doubleTap()
执行segue
这就是你不睡觉就工作到很晚的原因。这是最容易做的事。谢谢
注意:如果有人有更好的解决方案,或者这个解决方案在某些xyz情况下是错误的,请发表评论。如果你的更好,它将真正帮助他人。var lastClick:TimeInterval=0.0
var lastClick: TimeInterval = 0.0
var lastIndexPath: IndexPath? = nil
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let now: TimeInterval = Date().timeIntervalSince1970
if (now - lastClick < 0.3) && (lastIndexPath?.row == indexPath.row ) {
print("Double Tap!")
} else {
print("Single Tap!")
}
lastClick = now
lastIndexPath = indexPath
}
var lastIndexPath:IndexPath?=无
func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath)
{
现在让我们来看一下:TimeInterval=Date()。timeIntervalSince1970
如果(现在-lastClick<0.3)和&(lastIndexPath?.row==indexPath.row){
打印(“双击!”)
}否则{
打印(“单点击!”)
}
lastClick=现在
lastIndexPath=indepath
}
您可以通过委托传递cell对象,并通过以下方式找到indexpath:NSIndexPath*indexpath=[self.tableView indexPathForCell:cell];为我工作,谢谢!
var lastClick: TimeInterval = 0.0
var lastIndexPath: IndexPath? = nil
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let now: TimeInterval = Date().timeIntervalSince1970
if (now - lastClick < 0.3) && (lastIndexPath?.row == indexPath.row ) {
print("Double Tap!")
} else {
print("Single Tap!")
}
lastClick = now
lastIndexPath = indexPath
}