OGeek|极客世界-中国程序员成长平台

标题: ios - NSTimer与UITableView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:41
标题: ios - NSTimer与UITableView

我有带有“开始”按钮的UIView,带有我的UILabel时间的NSTimer和带有时间记录的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
                                         selectorselector(tick)
                                         userInfo:nil
                                          repeats:YES];
}

- (void)tick {

    _timeSot++;

    sot++;
/*some code*/

    myTime = [NSString stringWithFormat"%@:%@:%@", minStr, secStr, sotStr];


    [_timeLabel setText:myTime]; 
}



Best Answer-推荐答案


myTimer添加到NSRunLoopCommonModes:

myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f
                                           target:self
                                         selectorselector(tick)
                                         userInfo:nil
                                          repeats:YES];

 [[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSRunLoopCommonModes];

关于ios - NSTimer与UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24624497/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4