我有带有“开始”按钮的 UIView
、带有我的 NSTimer
时间的 UILabel
和带有时间记录的 UITableView
。
问题是当我滚动 tableview
- NSTimer
卡住时,为什么我可以让它们异步工作?
我的代码:
- (IBAction)startTimer {
_startButton.hidden = YES;
_stopButton.hidden = NO;
isRun = YES;
UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f
target:self
selector:@selector(tick)
userInfo:nil
repeats:YES];
}
- (void)tick {
_timeSot++;
sot++;
/*some code*/
myTime = [NSString stringWithFormat:@"%@:%@:%@", minStr, secStr, sotStr];
[_timeLabel setText:myTime];
}
最佳答案
将myTimer
添加到NSRunLoopCommonModes
:
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f
target:self
selector:@selector(tick)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSRunLoopCommonModes];
关于ios - NSTimer 与 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24624497/