title: Multiple cells actions UITableview
body: <p>I'm developing timer app and I made custom actions in cells for start/pause and reset timers. I used closures for this. This is my code for custom cell:</p>
  
  <p>My model:</p>
  
  <pre><code>class Timers: NSObject, Codable {
    var name = ""
    var id = ""
    var seconds = 0
    var editSeconds = 0
    var isStarted = false
  
  }
  </code></pre>
  
  <p>class TimerTableViewCell: UITableViewCell {</p>
  
  <pre><code>var startaction: (() -&gt; Void)? = nil
  var pauseaction: (() -&gt; Void)? = nil
  var reseteaction: (() -&gt; Void)? = nil
  
  @IBOutlet var timerLabel: UILabel!
  @IBOutlet var timerTime: UILabel!
  
  @IBOutlet var startPauseButton: UIButton!
  @IBOutlet var resetButton: UIButton!
  
  @IBAction func timerStart(_ sender: UIButton) {
      if let timerStart = self.startaction {
          timerStart()
      }
  }
  
   @IBAction func timerReset(_ sender: UIButton) {
       if let timerReset = self.reseteaction {
          timerReset()
      }
  }
  
  
  }
  </code></pre>
  
  <p>And this is part of my code for timers Controllers:</p>
  
  <pre><code>var timer = Timer()
  
  func runTimer(for cell: TimerTableViewCell, with item: Timers) {
  
      let content = UNMutableNotificationContent()
      content.title = item.name
      content.body = "Time is up!"
      content.sound = UNNotificationSound.default
      let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(item.seconds), repeats: false)
      let identifier = item.id
      let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
      let center = UNUserNotificationCenter.current()
      center.add(request, withCompletionHandler: nil)
  
      timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: {_ in
          if item.seconds &lt; 1 {
              self.timer.invalidate()
              let label = cell.viewWithTag(1001) as! UILabel
              let label2 = cell.viewWithTag(1000) as! UILabel
              label.textColor = .black
              label2.textColor = .black
              label.text = self.formattedTime(time: TimeInterval(item.seconds))
              item.isStarted = true
              cell.startPauseButton.setTitle("Play", for: .normal)
              self.audioPlayer.play()
              UIApplication.shared.isIdleTimerDisabled = false
          } else {
              item.seconds -= 1
              let label = cell.viewWithTag(1001) as! UILabel
              let label2 = cell.viewWithTag(1000) as! UILabel
              label.textColor = UIColor.init(red: 227.0/255.0, green: 61.0/255.0, blue: 39.0/255.0, alpha: 1.0)
              label2.textColor = UIColor.init(red: 227.0/255.0, green: 61.0/255.0, blue: 39.0/255.0, alpha: 1.0)
              label.text = self.formattedTime(time: TimeInterval(item.seconds))
              UIApplication.shared.isIdleTimerDisabled =  UserDefaults.standard.bool(forKey: "sleep")
          }
      })
          }
  
  
  
  // MARK: - Table view data source
  
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return timers.items.count
  }
  
  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "timer", for: indexPath) as! TimerTableViewCell
      let item = timers.items[indexPath.row]
      cell.timerLabel.text = item.name
      cell.timerTime.text = formattedTime(time: TimeInterval(item.seconds))
  
      if let color = UserDefaults.standard.colorForKey(key: "Color") {
          cell.startPauseButton.backgroundColor = color
          cell.resetButton.backgroundColor = color
      } else {
          cell.startPauseButton.backgroundColor = UIColor.init(red: 60.0/255.0, green: 60.0/255.0, blue: 60.0/255.0, alpha: 1.0)
          cell.resetButton.backgroundColor = UIColor.init(red: 60.0/255.0, green: 60.0/255.0, blue: 60.0/255.0, alpha: 1.0)
      }
  
      let secSettled = item.seconds
  
      cell.startaction = {
          if item.isStarted {
              self.timer.invalidate()
              item.isStarted = false
              cell.timerLabel.textColor = .gray
              cell.timerTime.textColor = .gray
              cell.startPauseButton.setTitle("Play", for: .normal)
  
              let center = UNUserNotificationCenter.current()
              center.removePendingNotificationRequests(withIdentifiers: [item.id])
          } else {
              self.runTimer(for: cell, with: item)
              item.isStarted = true
              cell.startPauseButton.setTitle("Pause", for: .normal)
              }
      }
  
      cell.reseteaction = {
          self.timer.invalidate()
          cell.timerLabel.textColor = .black
          cell.timerTime.textColor = .black
          if item.editSeconds &gt; 0 {
              cell.timerTime.text = self.formattedTime(time: TimeInterval(item.editSeconds))
              item.seconds = item.editSeconds
          } else {
              cell.timerTime.text = self.formattedTime(time: TimeInterval(secSettled))
              item.seconds = secSettled
          }
  
          item.isStarted = false
          cell.startPauseButton.setTitle("Play", for: .normal)
  
          let center = UNUserNotificationCenter.current()
          center.removePendingNotificationRequests(withIdentifiers: [item.id])
          }
  
      cell.showsReorderControl = true
  
      return cell
          }
  </code></pre>
  
  <p>When I run mulitple timers it start on multiple cells. How I can fix this problem? Can anybody help me with any solution?</p>
  
  <p><a href="https://i.stack.imgur.com/FBid9.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FBid9.gif" alt="enter image description here"></a></p>