iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UITableViewCell(Swift)中的多个计时器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有UITableViewcells,它们是在不同的时刻创建的,我希望它们中的每一个都有一个独立的计时器,在使用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,selector: #selector(LiveViewCell.processTimer),repeats: true)
        tableNumberLbl.text = "T" + String(NT.number)
        timeRemainingLbl.text = String(time)

    }
}

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

解决方法

覆盖prepareForReuse方法.

override func prepareForReuse() {
    super.prepareForReuse()

    timer.invalidate()
}

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

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

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

您必须做的最后一项更改是更改计时器,时间和blinkStatus,以便通过在类中移动它们来实例变量.

大佬总结

以上是大佬教程为你收集整理的ios – UITableViewCell(Swift)中的多个计时器全部内容,希望文章能够帮你解决ios – UITableViewCell(Swift)中的多个计时器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: