iOS中UITableView的每个单元格上的时间倒数

Dee*_*yan 2 uitableview ios swift

我想在tableview的每个单元格上显示倒数计时器。

脚步

我有来自服务器的具有过期时间的报价清单。因此,在此之后,我想与UITableView上的每个单元格一起显示此到期倒数计时器。

我正在做这样的事情。但是没有结果。

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell =  tableView.dequeueReusableCell(withIdentifier: "cell") as! TimeCell
        cell.expiryTimeInterval = 2

        return cell



    }
Run Code Online (Sandbox Code Playgroud)

**我正在启动计时器的单元格类别。仅打印步骤1和步骤**

class TimeCell: UITableViewCell {

    @IBOutlet weak var myLabel: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

    private var timer: Timer?
    private var timeCounter: Double = 0

    var expiryTimeInterval : TimeInterval? {
        didSet {
            startTimer()
            print("Step 1 \(expiryTimeInterval!)")
        }
    }

    private func startTimer() {
   if let interval = expiryTimeInterval {
            timeCounter = interval
             print("Step 2 \(interval)")
            if #available(iOS 10.0, *) {
                timer = Timer(timeInterval: 1.0,
                              repeats: true,
                              block: { [weak self] _ in
                                guard let strongSelf = self else {
                                    return
                                }
                                strongSelf.onComplete()
                })
            } else {
                timer = Timer(timeInterval: 1,
                              target: self,
                              selector: #selector(onComplete),
                              userInfo: nil,
                              repeats: true)
             }
        }
    }

    @objc func onComplete() {

        print("Step 3")
        guard timeCounter >= 0 else {
            timer?.invalidate()
            timer = nil
            return
        }
        myLabel.text = String(format: "%d", timeCounter)
        timeCounter -= 1
    }

}
Run Code Online (Sandbox Code Playgroud)

Uma*_*mir 5

我宁愿建议您创建一个自定义单元格,并在该单元格类中,根据服务器中的值创建一个计时器。这样,每个单元都有自己的计时器。那就是你可能想要的。在当前的实现中,您的计时器处理程序不知道单元格或其行号。

class MyCustomCell: UITableViewCell {
    @IBOutlet weak private var myLabel: UILabel!

    private var timer: Timer?
    private var timeCounter: Double = 0

    var expiryTimeInterval: TimeInterval? {
        didSet {
            startTimer()
        }
    }

    private func startTimer() {
        if let interval = expiryTimeInterval {
            timeCounter = interval
            if #available(iOS 10.0, *) {
                timer = Timer(timeInterval: 1.0,
                              repeats: true,
                              block: { [weak self] _ in
                                guard let strongSelf = self else {
                                    return
                                }
                                strongSelf.onComplete()
                })
            } else {
                timer = Timer(timeInterval: 1.0,
                              target: self,
                              selector: #selector(onComplete),
                              userInfo: nil,
                              repeats: true)
            }
        }
    }

    @objc func onComplete() {
        guard timeCounter >= 0 else {
            timer?.invalidate()
            timer = nil
            return
        }
        myLabel.text = String(format: "%d", timeCounter)
        timeCounter -= 1
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell =  tableView.dequeueReusableCell(withIdentifier: "cell") as! MyCustomCell

    cell.expiryTimeInterval = 10

    return cell
}
Run Code Online (Sandbox Code Playgroud)

转到您细胞nibstoryboard和更改类名TimeCellMyCustomCell