11

I'm a beginner and I'm trying to write and application for iOS that will show to the user in the main screen a timer from 25 minutes to 0 (made of a UILabel whom text is updated every second using an NSTimer) and a UITableView (embedded in the UIViewController of the main screen) that will show a cell for each time the timer as reached the 0.

So in the main view controlled by my UIViewController I have: - UILabel: for the timer - UITableView: wich shows a cell for each time the timer as reached 0 - UIButton: to start the timer

Everything seems to work fine except for the fact that scrolling the UITableView will stop the timer from updating the label as long as the user keeps the finger on the tablewView. Can anyone tell me why the label won't change its text while the UITableView is being scrolled?

P.S. I think that this problem could be related to the fact that I'm not using threads since I've not learned to use them yet.

piet.t
  • 11,718
  • 21
  • 43
  • 52
BigLex
  • 2,978
  • 5
  • 19
  • 27

4 Answers4

9

Solution in Swift 3.x:

self.updateTimer = Timer.scheduledTimer(timeInterval:1.0, target: self, selector: "updateFunction", userInfo: nil, repeats: true)
RunLoop.current.add(self.updateTimer, forMode: RunLoopMode.commonModes)
yo2bh
  • 1,356
  • 1
  • 14
  • 26
6
updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:p repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:updateTimer forMode:NSRunLoopCommonModes];
VvDPzZ
  • 2,995
  • 1
  • 23
  • 28
4

Timers don't fire during the tracking run loop mode, which is what you're in while scrolling. Check the NSTimer docs regarding run loop modes, or the WWDC 2012 session on scroll views, for details.

rickster
  • 124,678
  • 26
  • 272
  • 326
2

When scrolling UITableview, the timer does not repeat. We can solve this problem by adding a timer to RunLoop. For more detail, about RunLoop you can check the documentation.

A solution in Swift 4.1

self.updateTimer = Timer.scheduledTimer(timeInterval:1.0, target: self, selector: Selector(("updateFunction")), userInfo: nil, repeats: true)
RunLoop.current.add(self.updateTimer, forMode: RunLoopMode.common)
Antoine Boisier-Michaud
  • 1,575
  • 2
  • 16
  • 30
erdemsaid
  • 21
  • 5
  • Can you explain your solution further? – Antoine Boisier-Michaud Dec 12 '18 at 21:06
  • 2
    When scrolling UITableview, timer not repeat. We can solve this problem with adding timer to RunLoop. For more detail about RunLoop you can check this: [RunLoop](https://developer.apple.com/documentation/foundation/runloop) – erdemsaid Dec 13 '18 at 22:10