gpt4 book ai didi

ios - 尝试为 cellForRowAtIndexPath 重用计时器

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

所以我的目标是为每个单元重复使用计时器。除了一点之外,一切都运转良好。 cell.indexpath.row 从 1 而不是 0 开始。即使我滚动到顶部,它反弹回来,它也会变为 1。问题是计时器是基于 IndexPath 创建的。我已经尝试了很多增加单元格大小的解决方法,未能成功地尝试开始和结束更新路线,但似乎无法锁定它。基于我已有的代码的任何建议,或者新的解决方案有问题吗?

定时器VC

     var timer = Timer()
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

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

}

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

if let cell = tableView.dequeueReusableCell(withIdentifier: "liveCell", for: indexPath) as? liveActivitiesCell {
//print("VISBLE CELL \(tableView.indexPathsForVisibleRows)")
// var visibleCells = self.tableview.visibleCells
var prop = hudProp()
Prop = LiveActivityArray[indexPath.item]
cell.configureCell(properties: Prop, timer: &timer)
cell.property = Prop
cell.delegateFavorite = self
cell.delegate = self
cell.indexpath = indexPath
print("INDEX PATH CELL \(cell.indexpath.row)")

cell.StartTimerFilter(timer: &timer)

return cell
}
}
return UITableViewCell()

}

表格 View 单元

func configureCell(properties: hudProp, timer: inout Timer) {

if DataService.instance.BothDatesReturnedTrue == true {
timer.invalidate()
StartTimerFilter(timer: &timer)
}


@objc func TimerFuncFilter(timer: Timer) {

property.getAuctionTime(property.Begin, AuctionEnd: property.End, postAuctionStart: _leftLabel: leftTime, _leftLabelDayHour: leftTimeDaysHrs, _midLabel: midTime, _midLabelHourMin: midTimeHrsMin, _rightLabel: rightTime, _rightLabelMinSec: rightTimeMinSec, _timer: timer, EndsLabelFunc: EndsOnLabel, EndsDateLabelFunc: EndsOnDateLabel)
}
}
func StartTimerFilter(timer: inout Timer) {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(liveActivitiesCell.TimerFuncFilter), userInfo: nil, repeats: true)
DataService.instance.BothDatesReturnedTrue = false
}

因此,正如您所看到的,我不断使每个新单元格的计时器无效,但问题又是它从一个单元格开始。我可以继续使用这段代码并修改一些东西,还是应该重新考虑这个问题的架构。

提前谢谢您,我已经尝试解决这个问题好几个星期了,但有很多失败的解决方案。

最佳答案

因此,如果有人遇到困难或遇到类似情况,解决方案是在 View Controller 中不要有计时器,而是将其完全保留在您的 View 中。使一切正常工作而不会陷入许多计时器的保留周期的第二个方面是使用

func prepareForReuse() {
super.prepareForReuse
timer.invalidate
}

关于ios - 尝试为 cellForRowAtIndexPath 重用计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52408242/

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