首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何构建一个按钮来单独控制计时器并在标签中显示倒计时?

如何构建一个按钮来单独控制计时器并在标签中显示倒计时?
EN

Stack Overflow用户
提问于 2018-06-08 05:46:45
回答 1查看 622关注 0票数 0

我4个月前才学会Swift,我被一个问题卡住了,但我不知道如何解决它。我在StackOverflow、Youtube和Medium上看了很多教程,但我找不到一个像样的解决方案。

我的项目是:在ViewController中,有一个TableView,每个tableViewCell都有一个timerLabel、timerNameLabel、playButton和stopButton。当我单击playButton时,单元格的timeLabel将开始倒计时。当我单击stopButton时,计时器将停止。

我知道如何构建一个正确的计时器和一个正确的tableView:

viewController:

代码语言:javascript
复制
import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {



    var myTimerList = CustomTimerList()

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myTimerList.timerList.count
    }

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

}

TimerTableViewCell:

代码语言:javascript
复制
import UIKit

class TimerTableViewCell: UITableViewCell {

    var timer = Timer()

    @IBOutlet weak var timerName: UILabel!
    @IBOutlet weak var secondLeftLabel: UILabel!
    @IBAction func stopButton(_ sender: UIButton) {

        timer.invalidate()

    }

    @IBAction func playButton(_ sender: UIButton) {

        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(updateTimer)), userInfo: nil, repeats: true)

    }

    @objc func updateTimer() {



    }

}

TimerClass:

代码语言:javascript
复制
import Foundation

class TimerClass {


    let timerSecond : Int
    let timerName : String

    init(second:Int, name:String) {


        timerSecond = second
        timerName = name

    }

}

TimerList:

代码语言:javascript
复制
import Foundation

class CustomTimerList {

    var timerList = [TimerClass]()

    init() {

        let customTimer = TimerClass(second: 100, name: "AAA")
        timerList.append(customTimer)
        timerList.append(TimerClass(second: 200, name: "BBB"))
        timerList.append(TimerClass(second: 400, name: "CCC"))
        timerList.append(TimerClass(second: 150, name: "DDD"))
        timerList.append(TimerClass(second: 800, name: "EEE"))
        timerList.append(TimerClass(second: 1000, name: "FFF"))

    }

}

但我的问题是:

1.我尝试将runTimer放在tableViewCell中,并将我的playButton作为IBAction链接到tableViewCell.swift。这个按钮的内部是runTimer,但是我如何从TimerList获取数据并在timerLabel,中显示倒计时,因为我无法获取indexPath.row。

2.我尝试将runTimer放在viewController中,以便runTimer可以获取indexPath.row,但它有另一个问题:如何在timerLabel上显示运行定时器的结果并使其倒计时,,因为timerLabel是在tableViewCell而不是viewController中链接的。

我尝试了很多方法,比如协议,但我发现我不能正确地处理协议,所以我不知道如何正确地修改代码。

这是原始文件:https://app.box.com/s/0xviuay00pa2ief8b94ar5oh1002q3tn

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 06:03:20

您需要设置cellForRowAt

代码语言:javascript
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TimerTableViewCell

   let item = CustomTimerList.timerList[indexPath.row]
   cell.timerName.text = item.name
   cell.secondLeftLabel.text = "\(item.second)"

    return cell
}

//

代码语言:javascript
复制
struct CustomTimerList {

  static var timerList = [
      TimerClass(second: 100, name: "AAA"),
      TimerClass(second: 200, name: "BBB"),
      TimerClass(second: 400, name: "CCC"),
      TimerClass(second: 150, name: "DDD"),
      TimerClass(second: 800, name: "EEE"),
      TimerClass(second: 1000, name: "FFF")]

}

//

不要担心倒计时标签的当前内容并存储当前值,而是从文本中获取当前值,如果不是零,则将其减去并重新设置,也不要认为如果用户在cellForRowAt中的此行之后滚动,则当前没有计时器的停止机制

代码语言:javascript
复制
 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TimerTableViewCell
 cell.timer?.invalidate()

//

代码语言:javascript
复制
class TimerTableViewCell: UITableViewCell {
    var timer:Timer?
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50750595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档