UITableViewCell (Swift) 中的多个计时器 [英] Multiple timers in UITableViewCell (Swift)

查看:58
本文介绍了UITableViewCell (Swift) 中的多个计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在不同时刻创建的 UITableViewcells,我希望它们中的每一个都有一个独立的计时器,当使用 reloadData() 添加对象时触发该计时器代码>

I have UITableViewcells that are created at different moments in time and I would like each one of them to have an independent timer that triggers when the object is added with reloadData()

这是我目前所做的

import UIKit

var timer = Timer()
var blinkStatus:Bool! = false
var time = 300


class LiveViewCell: UITableViewCell {

    let str = String(format:"%02d:%02d", (time / 60), (time % 100))
    func processTimer() {

        if time > 0 {
            time -= 1
            timeRemainingLbl.text = String(time)
        } else if time == 0 {
            timer.invalidate()
            timeRemainingLbl.text = "Due!"
            timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(LiveViewCell.blinkTimer), userInfo: nil, repeats: true)

        }

    }

    func blinkTimer() {
        if blinkStatus == false {
            timeRemainingLbl.textColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:1.0)
            blinkStatus = true
        } else if blinkStatus == true {
            timeRemainingLbl.textColor = UIColor.clear
            blinkStatus = false
        }

    }


    @IBOutlet weak var tableNumberLeftLabel: UILabel!
    @IBOutlet weak var guestNumbersLabel: UILabel!

    @IBOutlet weak var timeInTableLabel: UILabel!
    @IBOutlet weak var tableNumberLbl: UILabel!
    @IBOutlet weak var timeRemainingLbl: UILabel!


    var table: Table!


    func configureLiveCell(_ NT: Table) {

        layer.cornerRadius = 20
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(LiveViewCell.processTimer), userInfo: nil, repeats: true)
        tableNumberLbl.text = "T" + String(NT.number)
        timeRemainingLbl.text = String(time)

    }
}

每当我创建一个新单元格时,都会调用 configureLiveCell 时出现问题.计时器似乎加快了速度,我希望每个计时器都是独立的.

The problem comes when configureLiveCell gets called whenever I create a new cell. The timer seems to speed up and I would like each timer to be independent.

推荐答案

覆盖 prepareForReuse 方法.

override func prepareForReuse() {
    super.prepareForReuse()

    timer.invalidate()
}

这将在单元格返回以供另一行使用之前调用.通过在此处使计时器无效,您的 configureLiveCell 不会再创建另一个计时器.

This will be called just before a cell is returned to be used by another row. By invalidating the timer here, your configureLiveCell doesn't create yet another timer.

顺便说一句 - 您还应该添加一个 deinit 方法并使计时器无效.

BTW - you should also add a deinit method and invalidate the timer there too.

您还可以将 timer 属性设为可选,并在使其无效后将其设置为 nil.当然,您需要添加适当的检查来处理它是可选的.

You also make the timer property optional and set it to nil after you invalidate it. And of course you need to add proper checks to deal with it being an optional.

您必须进行的最后一项更改是更改 timertimeblinkStatus,使它们成为实例变量,方法是将它们移动到班级.

And one last change you must make is to change timer, time, and blinkStatus so they are instance variables by moving them inside the class.

这篇关于UITableViewCell (Swift) 中的多个计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆