gpt4 book ai didi

ios - 滚动 UITableView 期间计时器将重置并停止运行

转载 作者:行者123 更新时间:2023-11-30 11:25:40 25 4
gpt4 key购买 nike

timer.gif

↑↑↑↑↑↑↑↑↑↑我在这个GIF中展示了问题↑↑↑↑↑↑

tableView单元格中有一个计时器和stratButton,当我单击按钮时,timeLabel将开始运行。

现在的问题是,当我将正在运行的计时器滚动到屏幕外并将其滚动回来时,计时器将重置并停止。

希望有人能帮助我!我检查了很多解决方案,但它们对我不起作用!我都快哭了。

我的cellForRow:

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

let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! TimerTableViewCell

cell.timer?.invalidate()
let item = myTimerList[indexPath.row]
cell.timerName.text = item.timerName
cell.secondLeftLabel.text = "\(item.timerSecond)"
return cell

}

我创建了一个小项目,其中包含我的代码供您修改:https://app.box.com/s/axluqkjg0f7zjyigdmx1lau0c9m3ka47

最佳答案

您需要保留每个单元格的状态

class Service {

static let shared = Service()

var myTimerList = [TimerClass]()
}

//

这里我添加了另外 2 个变量,为什么尽管您必须以相同的方式初始化它们,但仍保留 timerName 因为 current 将保存不断变化的值

class TimerClass {

let timerSecond : Int
let timerName : String
var current: Int
var isPlaying = false

init(second:Int, name:String) {

timerSecond = second
timerName = name
current = second
}

}

//

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

let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! TimerTableViewCell


let item = Service.shared.myTimerList[indexPath.row]
cell.tag = indexPath.row // to access the array inside the cell
if item.isPlaying {
cell.play() // add play method to the cell it has same button play action
}
else {
cell.timer?.invalidate()
}
cell.timerName.text = item.timerName
cell.secondLeftLabel.text = "\(item.current)"
return cell

}

//

在单元格内单击按钮时,将 isPlaying 属性更改为 true,停止时更改为 false,如下所示

// here self is the cell itself 

Service.shared.myTimerList[self.tag].isPlaying = true

当计时器滴答声发生变化时

Service.shared.myTimerList[self.tag].current = // value 

关于ios - 滚动 UITableView 期间计时器将重置并停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50765114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com