自定义 tableViewCell 中的 NSTimer

NSTimer inside custom tableViewCell

我正在从 viewController 激活自定义单元格 class 中的一个功能。自定义单元格 class 如下所示:

import UIKit

class TableViewCell: UITableViewCell {

    var counter = 10

    class func timerStarted(){
        var timer = NSTimer()

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)
    }

    class func update(){

        let cell = TableViewCell()
        var count = cell.counter
        count = --count
        println(counter)
    }
}

问题是变量counter没有变化,所以每间隔打印9。我如何让它每次都改变值并倒计时?

如有任何建议,我们将不胜感激。

编辑:我正在使用长按手势识别器来触发该功能,这就是我不能仅使用 didSelectRowAtIndexPath 功能触发它的原因。我的长按代码如下所示:

func longPressActive(gestureRecognizer:UIGestureRecognizer) {
    if (gestureRecognizer.state == UIGestureRecognizerState.Began) {
        var point = gestureRecognizer.locationInView(self.tv)
        if let indexPath = self.tv.indexPathForRowAtPoint(point) {
            TableViewCell.timerStarted()
        }
    }
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as TableViewCell
    cell.timerStarted()
}

对于您的表格视图单元格 class:

func timerStarted(){
    var timer = NSTimer()

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)
}

func update(){
    counter = counter - 1
    println(counter)
}

好的,所以您的问题是您在 TableView class 上调用 class 方法而不是实例函数。您想要获取实际单元格实例的句柄,而不仅仅是 class。因此,首先,您的 TableCell class 具有正确的签名(即删除 class 前缀):

class TableViewCell: UITableViewCell {

    var counter = 10

    // No longer class functions! :)
    func timerStarted(){
        var timer = NSTimer()

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)
    }

    func update() {
        // Update counter
        counter-- // <-- performs the actual decrement for you
        println(counter)
    }
}

然后只需更新您的长按即可激活实际单元格上的计时器,而不仅仅是单元格的 class:

func longPressActive(gestureRecognizer:UIGestureRecognizer) {
    if (gestureRecognizer.state == UIGestureRecognizerState.Began) {
        var point = gestureRecognizer.locationInView(self.tv)
        if let indexPath = self.tv.indexPathForRowAtPoint(point) {
            // Check to make sure it is the correct subclass
            if let cell = self.tv.cellForRowAtIndexPath(indexPath: indexPath) as? TableViewCell {
                // Starting the timer on the actual cell, not just the cell class
                cell.timerStarted();
            }
        }
    }
}

此外,我想对您的 timerStarted() 功能发表评论。您首先创建一个新计时器并将其分配给 timer,然后您创建第二个计时器并将其也分配给 timer,这是多余的。此外,由于您没有在该方法之外保存计时器,因此无需创建变量(以保持相同的功能)。所以函数可以是:

func timerStarted(){
    NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)
}

但是你很有可能会在某个时候取消它,所以我可能会将它存储为一个实例变量:

private var timer: NSTimer

func timerStarted(){
    self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)
}