ios - 在选择器中为 scheduleTimerwithTimeInterval 传递参数

标签 ios objective-c swift selector

在 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/

相关文章:

ios - 尝试使用警报完成刷新 Collection View

iphone - 一个接一个地播放音频文件?

ios - 处理没有互联网连接的语音错误

ios - iOs 8 上的 Twitter API 有什么问题?

ios - didUpdateLocations 未被调用

swift - 调整 Swift 代码以与 Swift 3 配合使用

ios - 有没有一种方法可以控制图像选择器完成后转到哪个 View Controller

ios - 检测主视图 Controller 和详细 View Controller 何时都在屏幕上?

c++ - C++ 中的 Objective-C @protocol 等价物

ios - 使用 3 个 UITableviewcontroller 作为值列表