在 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) {}
但这给了我一个错误,因为选择器不喜欢这种语法。有没有办法让选择器工作或有更好的方法?
谢谢,
最佳答案
通常的做法是通过NSTimer
参数来实现action
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)
关于ios - 在选择器中为 scheduleTimerwithTimeInterval 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38327282/