在桌面视图中使用计时器重新加载任何滚动或表后,创build计时器

我使用https://github.com/mineschan/MZTimerLabel/,并在我的Tableview cellForRowAtIndex使用下面的计时器:

UILabel *lblTimer=(UILabel *)[cell viewWithTag:10]; MZTimerLabel *UpgradeTimer = [[MZTimerLabel alloc] initWithLabel:lblTimer andTimerType:MZTimerLabelTypeTimer]; [UpgradeTimer setCountDownTime:timestamp]; [UpgradeTimer startWithEndingBlock:^(NSTimeInterval timestamp) { lblTimer.text = @"✔"; }]; 

但是,在任何表重装或滚动后,计时器的行为很奇怪,似乎重新生成多个计时器在同一个地方进行计数。 在使用这个定时器的时候我应该如何解决

感谢任何帮助,

埃利亚斯

我看了一下MZTimerLabel ,它严重违反了MVC。 它把属于模型的东西(计数倒数的定时器)放到视图中。 这是你的问题来自何处。 视图应该能够被重新创build而不会对模型产生副作用。

我会build议沟通这个class级,并创build自己的。 实现这样的事情其实很简单。

  1. 创build一个保存标题和结束date的新类
  2. 将该类的实例存储在支持您的表的模型中
  3. 创build一个刷新tableView的NSTimer
  4. 设置你的单元格。

这基本上是您在表格中进行基本倒计时所需的所有代码。 因为它不会在视图中存储任何数据,所以您可以尽可能多地进行滚动:

 @interface Timer : NSObject @property (strong, nonatomic) NSDate *endDate; @property (strong, nonatomic) NSString *title; @end @implementation Timer @end @interface MasterViewController () { NSArray *_objects; NSTimer *_refreshTimer; } @end @implementation MasterViewController - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *modelStore = [NSMutableArray arrayWithCapacity:30]; for (NSInteger i = 0; i < 30; i++) { Timer *timer = [[Timer alloc] init]; timer.endDate = [NSDate dateWithTimeIntervalSinceNow:i*30]; timer.title = [NSString stringWithFormat:@"Timer %ld seconds", (long)i*30]; [modelStore addObject:timer]; } _objects = modelStore; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [_refreshTimer invalidate]; // timer should not exist, but just in case. _refreshTimer = [NSTimer timerWithTimeInterval:0.5f target:self selector:@selector(refreshView:) userInfo:nil repeats:YES]; // should fire while scrolling, so we need to add the timer manually: [[NSRunLoop currentRunLoop] addTimer:_refreshTimer forMode:NSRunLoopCommonModes]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [_refreshTimer invalidate]; _refreshTimer = nil; } - (void)refreshView:(NSTimer *)timer { // only refresh visible cells for (UITableViewCell *cell in [self.tableView visibleCells]) { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; [self configureCell:cell forRowAtIndexPath:indexPath]; } } #pragma mark - Table View - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _objects.count; } - (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { Timer *timer = _objects[indexPath.row]; cell.textLabel.text = timer.title; NSInteger timeUntilEnd = (NSInteger)[timer.endDate timeIntervalSinceDate:[NSDate date]]; if (timeUntilEnd <= 0) { cell.detailTextLabel.text = @"Finished"; } else { NSInteger seconds = timeUntilEnd % 60; NSInteger minutes = (timeUntilEnd / 60) % 60; NSInteger hours = (timeUntilEnd / 3600); cell.detailTextLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", (long)hours, (long)minutes, (long)seconds]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; [self configureCell:cell forRowAtIndexPath:indexPath]; return cell; } @end 

在这里输入图像说明