【问题标题】:Scrolling an UITableView will stop Timer [duplicate]滚动 UITableView 将停止 Timer [重复]
【发布时间】:2012-08-22 02:52:24
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: iphone objective-c ios xcode


    【解决方案1】:

    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)
    

    【讨论】:

      【解决方案2】:
      updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:p repeats:YES];
      [[NSRunLoop currentRunLoop] addTimer:updateTimer forMode:NSRunLoopCommonModes];
      

      【讨论】:

      • 你应该详细说明你的解决方案。
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        滚动 UITableview 时,计时器不重复。我们可以通过在 RunLoop 中添加一个定时器来解决这个问题。更多细节,关于RunLoop你可以查看the documentation。

        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)
        

        【讨论】:

        • 您能进一步解释您的解决方案吗?
        • 滚动 UITableview 时,计时器不重复。我们可以通过在 RunLoop 中添加定时器来解决这个问题。有关 RunLoop 的更多详细信息,您可以查看:RunLoop
        猜你喜欢
        • 2012-03-23
        • 2014-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 2011-01-13
        • 1970-01-01
        相关资源
        最近更新 更多