当与NSTimer一起使用时,cellForRowAtIndexPath无法正确显示单元格

时间:2019-03-19 20:00:43

标签: objective-c uitableview nstimer

我的情况很奇怪。我的桌子上有照片。我可以使用某些按钮在tableview中添加新照片。在出现在tableview上之前,单元格应随机显示progressView的秒数。为此,我正在使用NSTimer。一切正常,除了同时添加多个图像时,如果我向上滚动,它还在某些已经加载的单元格中显示progressView加载。我通过NSLog检查了if条件(开始加载的唯一方法),它仅显示正在加载单元格的索引。我感谢您的帮助:) 这是cellForRowAtIndexPath的代码:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"photoCell"];

PhotoCell *photoCell = (PhotoCell *)cell;
//if there is active timer
if([self.activeTimerIndexArray containsObject:indexPath]){
    [photoCell.resultImageV setHidden:YES];

    UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
    progressView.frame = CGRectMake(photoCell.resultImageV.frame.origin.x, photoCell.resultImageV.frame.size.height/2, photoCell.resultImageV.frame.size.width, photoCell.resultImageV.frame.size.height);
    [progressView setTag:11];
        [photoCell.contentView addSubview:progressView];
}
UIImage *img = (UIImage *)[self.resultArray objectAtIndex:indexPath.row];
photoCell.resultImageV.image = img;
return cell;

这是updateTimer方法的代码:

Timer *timerInfo = timer.userInfo;
NSIndexPath *index = timerInfo.forIndex;
PhotoCell *cell = [self.tableView cellForRowAtIndexPath:index];
if(timerInfo.time >= timerInfo.totalTime)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIProgressView *prg = [cell viewWithTag:11];
        [prg removeFromSuperview];
        [cell.resultImageV setHidden:NO];
    });
    [timer invalidate];
    //no longer active timer
    [self.activeTimerIndexArray removeObject:timerInfo.forIndex];
}
else
{
    timerInfo.time += 0.01;
    float progress = timerInfo.time/timerInfo.totalTime;
    dispatch_async(dispatch_get_main_queue(), ^{
            UIProgressView *prg = [cell viewWithTag:11];
            prg.progress = progress;
    });
}

更新

由于@vadian,我更新了模型,为计时器创建了简单的类,并如上所述使用了timerInfo数组。

0 个答案:

没有答案