在swift中,我有一个uitableviewCell,它实现了双击和单击。双击工作。但是,单击时我遇到了一些麻烦。由于双击的现在,我用计时器实现了单击。以下代码成功打印出" Single Tapped"
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var now = NSDate().timeIntervalSince1970
if (now - lastClick < 0.3) && indexPath.isEqual(lastIndexPath) {
// Double tapped on cell
if let cell = discoverTableView.cellForRowAtIndexPath(indexPath) as? CommentCell {
cell.doubleTapCellActive({ (addBumpStatus, success) in
})
}
singleTapTimer?.invalidate()
} else {
singleTapTimer = NSTimer.scheduledTimerWithTimeInterval(0.31, target: self, selector: #selector(DiscoverVC.singleTapped), userInfo: nil, repeats: false)
}
lastClick = now
lastIndexPath = indexPath
}
func singleTapped() {
print("Single tapped")
}
然而,问题是,我希望单击以了解选择了哪个索引路径。我尝试过像
这样的事情#selector(DiscoverVC.singleTapped(_:indexPath))
func singleTapped(indexPath: NSIndexPath) {}
但这会给我一个错误,因为选择器不喜欢这种语法。有没有办法让选择器工作或更好的方式呢?
谢谢,
答案 0 :(得分:1)
通常的方法是使用NSTimer
参数
func singleTapped(timer : NSTimer) {
print("Single tapped", timer.userInfo!["indexPath"] as! NSIndexPath)
}
并通过计时器的userInfo
属性传递自定义数据,例如
singleTapTimer = NSTimer.scheduledTimerWithTimeInterval(0.31,
target: self,
selector: #selector(singleTapped(_:)),
userInfo: ["indexPath":indexPath],
repeats: false)