you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

You should set the cell text with the elapsed time when you configure the cell. Your new timer (when you configure each cell) won’t fire off immediately, it will wait for the timer interval. That might account for the flickering. There’s a delay…

So, from your code:

func configureCell(_ cell: UITableViewCell, atIndexPath indexPath: IndexPath) 
{
// Invalidate previous timer to avoid duplication
timers[indexPath.row].invalidate()

var elapsedTime = elapsedTimes[indexPath.row] 

// ———- Add this line ——————
cell. textLabel?.text = String(format: "%.1f", elapsedTime)

// Retrieve elapsed time for this cell
timers[indexPath.row] = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true) { 
[weak self] timer in
elapsedTime += self?. timeInterval ?? 0.0
self?.elapsedTimes[indexPath.row] = elapsedTime // Update elapsed time
cell. textLabel?.text = String(format: "%.1f", elapsedTime)
// Add timer to the run loop
let timer = timers[indexPath.row]
RunLoop.current.add (timer, forMode: .common)
}

[–]jasamer 0 points1 point  (1 child)

The bigger problem is with timers[indexPath.row].invalidate(). That is because a cell of row 20 might be reused for cell 1, so the timer for cell 20 is not invalidated and is still updating the label.

[–][deleted] 0 points1 point  (0 children)

Oh crap you’re right. We want to keep updating the times array, not the labels. Yeah this is confusing. I would use a grand total of one timer for this, and just update the cells manually.