9

我是一个初学者,我正在尝试为 iOS 编写和应用程序,它将在主屏幕上向用户显示一个从 25 分钟到 0 的计时器(由一个UILabel文本每秒更新一次的人组成NSTimer)和一个UITableView(嵌入在UIViewController主屏幕中),每次计时器达到 0 时都会显示一个单元格。

所以在我控制的主视图中,UIViewController我有: - UILabel:对于计时器 - UITableView:每次计时器达到 0 时显示一个单元格 - UIButton:启动计时器

一切似乎工作正常,除了滚动UITableView将停止计时器更新标签,只要用户将手指放在 tablewView 上。谁能告诉我为什么标签UITableView在滚动时不会更改其文本?

PS我认为这个问题可能与我没有使用线程的事实有关,因为我还没有学会使用它们。

4

4 回答 4

8

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)
于 2017-08-19T06:52:16.013 回答
6
updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:p repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:updateTimer forMode:NSRunLoopCommonModes];
于 2013-01-22T03:08:40.277 回答
4

计时器在跟踪运行循环模式期间不会触发,这是您在滚动时所处的状态。查看有关运行循环模式的 NSTimer 文档,或有关滚动视图的 WWDC 2012 会话,了解详细信息。

于 2012-08-27T15:38:42.730 回答
2

滚动 UITableview 时,计时器不会重复。我们可以通过在 RunLoop 中添加一个定时器来解决这个问题。有关更多详细信息,您可以查看有关 RunLoop的文档

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)
于 2018-12-12T20:45:18.373 回答