我在UITableViewCell的子类中创建了一个计时器函数,它使用计时器更新自身的标签(标签文本):
func handleCountdown(){
if timerIsRunning == false { //check if timer is NOT running, otherwise update timer
timerIsRunning = true
startTime = activity.countdownValue
runTimer()
}
}
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: (#selector(self.updateTimer)), userInfo: nil, repeats: true)
}
@objc func updateTimer(){
let hours = Int(startTime) / 3600
let minutes = Int(startTime) / 60 % 60
if startTime > 0 {
startTime -= 1
print("The start time for " + String(activity.name) + "is " + String(startTime))
switch startTime {
case 0:
activity.isActive = false
activity.timerIsRunning = false
activity.countdownValue = 0
timer.invalidate()
activitiesRef = Database.database().reference().child("Users/\(userID)/Activities")
activitiesRef.child(self.activity.id).setValue([ "id": self.activity.id,
"name": self.activity.name,
"isActive": self.activity.isActive,
"locString": self.activity.locationString,
"locLat": self.activity.locLat,
"locLong": self.activity.locLong,
"privacySetting": self.activity.privacySetting,
"timerIsRunning": self.activity.timerIsRunning,
"countdownValue": self.activity.countdownValue])
returnToInactiveDelegate?.activeToInactive(data: activity)
case 1..<60: //display seconds
let count = String(Int(startTime))
countdownTimer.text = count
print("The start time for " + String(activity.name) + "is " + String(startTime))
//countdownTimer.text = String(Int(startTime)) + "s"
case 60..<3600: //display minutes
print("The start time for " + String(activity.name) + "is " + String(startTime))
countdownTimer.text = String(Int(minutes)) + "m"
case 3600..<86400: // display hours and minutes
print("The start time for " + String(activity.name) + "is " + String(startTime))
countdownTimer.text = String(Int(hours)) + "h \(minutes)m"
default:
timer.invalidate()
activity.countdownValue = 0
}
}
当计时器到达零时,委托函数删除相应的单元格并将其移动到表格视图中的不同部分,如下所示:
func activeToInactive(data: Activity) {
if let i = activeActivities.index(where: { $0.isActive == false }) {
print("array index to remove: should be where it is NOT active = \(i)")
activeActivities.remove(at: i)
}
inactiveActivities.insert(data, at: 0)
self.tableView.reloadData()
}
最后,在表视图重新加载时,它会进行最后一次检查以确定计时器是否仍在运行(看它是否应该从初始启动时间启动计时器,或者不执行任何操作并让它处理倒计时)如下:
let inactiveCell = tableView.dequeueReusableCell(withIdentifier: inactiveIdentifer , for: indexPath) as! InactiveCell
let activeCell = tableView.dequeueReusableCell(withIdentifier: activeIdentifier, for: indexPath) as! ActiveCell
if indexPath.section == 0 && self.activeActivities.count != 0 {
activeCell.name.text = self.activeActivities[indexPath.row].name
activeCell.location.text = self.activeActivities[indexPath.row].locationString
activeCell.activity = self.activeActivities[indexPath.row]
activeCell.returnToInactiveDelegate = self
if activeCell.activity.isActive == true && activeCell.timerIsRunning == false {
activeCell.activity.countdownValue = self.activeActivities[indexPath.row].countdownValue
print(String(self.activeActivities[indexPath.row].countdownValue))
activeCell.handleCountdown()
}
activeCell.countdownTimer.text = String(self.activeActivities[indexPath.row].countdownValue)
//activeCell.countdownTimer.text = self.activeActivities[indexPath.row].
return activeCell
我遇到的问题是倒计时标签文本没有更新,尽管它在终端控制台中显示正确的输出。通过此示例我的错误是可重复的:有两个活动活动,其中倒计时功能相互独立。
activeActivity[0]
运行为零,然后由ojbc func updateTimer中的case 0处理,并通过委托传递,然后将其从active中删除并将其移至inactive。剩下的一个(以前是activeActivity[1]
,现在是activeActivity[0]
)移动&#34; up&#34;在队列中,计时器根据终端日志继续运行,但countdownLabel.text卡在1s,尽管终端显示updateTimer的执行,并打印出应该在表格视图单元格中显示的正确开始时间。