Uitableview iOS:当用户滚动表格视图时,标签停止更新

Uitableview iOS:当用户滚动表格视图时,标签停止更新,uitableview,ios7,xcode5,Uitableview,Ios7,Xcode5,在一个视图中,我得到了一个标签和一个表视图。标签通过计时器每秒更新一次,计时器调用更新标签文本的函数。现在一切正常。但一旦用户用手指在表视图上滑动,标签的文本就会停止更新 有没有办法防止这种行为?滚动tableview时,标签会停止更新,因为计时器和tableview都位于同一线程(即主线程)上。您可以尝试下面的代码 这两种方法用于更新UILabel,与tableView滚动无关 - (void)viewDidLoad { [super viewDidLoad]; // Do any addit

在一个视图中,我得到了一个标签和一个表视图。标签通过计时器每秒更新一次,计时器调用更新标签文本的函数。现在一切正常。但一旦用户用手指在表视图上滑动,标签的文本就会停止更新


有没有办法防止这种行为?

滚动tableview时,标签会停止更新,因为计时器和tableview都位于同一线程(即主线程)上。您可以尝试下面的代码

这两种方法用于更新UILabel,与tableView滚动无关

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

arrData = [NSMutableArray new];

for (int i = 0; i < 150; i ++) {
    [arrData addObject:[NSString stringWithFormat:@"Row number %d",i]];
}

[self performCounterTask];
count = 10;

}


-(void)performCounterTask{

if (count == 0) {
    count = 10;
}

double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

    // Your code here

    if (count >= 0) {
        [lblTimer setText:[NSString stringWithFormat:@"%ld",(long)count]];
        count--;

        [self performCounterTask];

    }


});


}
所以,基本上您需要将标签更新部分保持在调度队列下

希望这个解决方案能对您有所帮助。快乐编码:)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arrData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.textLabel.text = [arrData objectAtIndex:indexPath.row];

return cell;

}