Здравствуйте. Не подскажите, как можно запускать одновременно несколько таймеров для каждой ячейки таблицы в UITableView и чтобы они не смешивались между собой?
Я запускаю и останавливаю таймеры в didSelectRowAt:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let cell = tableView.cellForRow(at: indexPath) as! TimerTableViewCell
let item = timers.items[indexPath.row]
item.toggle()
print(item)
startPauseTimer(for: cell, with: item)
}
```
Это код для старта и остановки таймеров:
```
var timer = Timer()
func startPauseTimer(for cell: TimerTableViewCell, with item: Timers) {
if !item.isStarted {
timer.invalidate()
} else {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {timer in
item.seconds -= 1
cell.timerTime.text = self.formattedTime(time: TimeInterval(item.seconds))
if item.seconds < 1 {
self.timer.invalidate()
cell.timerTime.text = self.formattedTime(time: TimeInterval(item.seconds))
item.isStarted = false
}
}
}
}
```
И дата модель:
class Timers: NSObject, Codable {
var name = ""
var id = ""
var seconds = 0
var editSeconds = 0
var isStarted = false
func toggle() {
isStarted = !isStarted
}
```