我4个月前才学会Swift,我被一个问题卡住了,但我不知道如何解决它。我在StackOverflow、Youtube和Medium上看了很多教程,但我找不到一个像样的解决方案。
我的项目是:在ViewController中,有一个TableView,每个tableViewCell都有一个timerLabel、timerNameLabel、playButton和stopButton。当我单击playButton时,单元格的timeLabel将开始倒计时。当我单击stopButton时,计时器将停止。
我知道如何构建一个正确的计时器和一个正确的tableView:
viewController:
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:
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:
import Foundation
class TimerClass {
let timerSecond : Int
let timerName : String
init(second:Int, name:String) {
timerSecond = second
timerName = name
}
}
TimerList:
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
发布于 2018-06-08 06:03:20
您需要设置cellForRowAt
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
}
//
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
中的此行之后滚动,则当前没有计时器的停止机制
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TimerTableViewCell
cell.timer?.invalidate()
//
class TimerTableViewCell: UITableViewCell {
var timer:Timer?
}
https://stackoverflow.com/questions/50750595
复制相似问题