简体   繁体   中英

how to remove cell index when timer gets complete after 5 min ios swift 5 , when called api not repeat timeragain of same index

I want to implement timer logic, when 5 min gets complete then my Tableview reload and its remove that particular index, I have tried not gets works, and timer get fast

//Timer ACtion Method
 @objc func timerAction() {
        
          if seconds>0 {
            seconds-=1
            minutes = String(format:"%02i",(seconds / 60))
            seconds1 = String(format:"%02i",(seconds % 60))
            print(minutes! + ":" + seconds1!)
            self.lblMin.text = minutes!
            self.lblSec.text = seconds1!
          } else {
              minutes = String(seconds / 60)
              seconds1 = String(seconds % 60)
              if minutes == "0" && seconds1 == "0" {
              
                timer.invalidate()
                btnReject.isUserInteractionEnabled = false
                btnAccept.isUserInteractionEnabled = false
               // TBVC.InstancePending.arrPending.remove(at: intValue!)
                //tblData?.deleteRows(at: [IndexPath(row: intValue!, section: 1)], with: .automatic)
               // TBVC.InstancePending.getTableBooking(strStatus: "0")
               // TBVC.InstancePending.strTap = "Pending"
               // TBVC.InstancePending.segment.selectedSegmentIndex = 0
                
               // tblData?.reloadData()
                
              }
            }
         }

Set Timer value nil, And check when API called then the timer will not pass any selector method

@objc func timerAction() {
    
      if seconds>0 {
        seconds-=1
        minutes = String(format:"%02i",(seconds / 60))
        seconds1 = String(format:"%02i",(seconds % 60))
        print(minutes! + ":" + seconds1!)
        self.lblMin.text = minutes!
        self.lblSec.text = seconds1!
      } else {
          minutes = String(seconds / 60)
          seconds1 = String(seconds % 60)
          if minutes == "0" && seconds1 == "0" {
          
            timer.invalidate()
            timer = nil
            btnReject.isUserInteractionEnabled = false
            btnAccept.isUserInteractionEnabled = false
           // TBVC.InstancePending.arrPending.remove(at: intValue!)
            //tblData?.deleteRows(at: [IndexPath(row: intValue!, section: 1)], with: .automatic)
           // TBVC.InstancePending.getTableBooking(strStatus: "0")
           // TBVC.InstancePending.strTap = "Pending"
           // TBVC.InstancePending.segment.selectedSegmentIndex = 0
            
           // tblData?.reloadData()
            
          }
        }
     }

=====================================

2nd method to implement timer:-

Initialize Variable

var timer:Timer?
var totalMinut:Int = 2
var totalSecond:Int = 120
var timeLeft = 120

Add timer function

 func setupTimer() {
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
}

@objc func onTimerFires() {
    
       var minutes: Int
       var seconds: Int

       if totalSecond == 1 {
           timer?.invalidate()
           timer = nil
       }
       totalSecond = totalSecond - 1
       minutes = (totalSecond) / 60
       seconds = (totalSecond) % 60
       timerLabel.text = String(format: "%02d:%02d", minutes, seconds)
}

Call "setUpTimer" method where you have required. In my case, I have called it in the "viewDidLoad" method of a view controller

 override func viewDidLoad() {
    super.viewDidLoad()
 
    setupTimer()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM