NSTimer里面有自定义的uitableViewCell

时间:2015-01-14 15:41:58

标签: ios class swift nstimer custom-cell

我正在从viewController激活我的自定义单元格类中的函数。自定义单元类如下所示:

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)
    }
}

问题是变量计数器不会改变,因此每个间隔打印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()
        }
    }
}

2 个答案:

答案 0 :(得分:3)

好吧,你的问题是你在TableView类而不是实例函数上调用类方法。您希望获得实际单元实例的句柄,而不仅仅是类。首先,您的TableCell类具有适当的签名(即删除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)
    }
}

然后只需更新你的长按以激活实际单元格上的计时器,而不仅仅是单元格的类:

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)
}

答案 1 :(得分:1)

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as TableViewCell
    cell.timerStarted()
}

对于tableview单元类:

func timerStarted(){
    var timer = NSTimer()

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

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