L'utilisation de la timer dans une vue de table crée le minuteur après un défilement ou une recharge de table

J'utilise https://github.com/mineschan/MZTimerLabel/ et dans mon TableView cellForRowAtIndex en utilisant le minuteur comme ci-dessous:

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

Mais après le rechargement ou le défilement de la table, le minuteur se comporte étrangement et semble générer plusieurs minuteurs pour le comptage au même endroit. Comment dois-je résoudre ce problème en utilisant cette timer?

Appréciez toute aide,

Elias

J'ai jeté un oeil à MZTimerLabel , et ça viole mal MVC. Il met quelque chose qui appartient au model (le count à rebours est en baisse) dans la vue. C'est de là que vient votre problème. Les vues doivent pouvoir être recréées sans avoir d'effets secondaires sur le model.

Je reorderais d'abandonner cette class et de créer la vôtre. C'est en fait assez facile de réaliser quelque chose comme ça.

  1. Créer une nouvelle class qui enregistre un titre et une fin
  2. Stocker les instances de cette class dans le model qui sauvegarde votre table
  3. Créez un NSTimer qui actualise la tableView
  4. Configurez vos cellules.

C'est essentiellement tout le code dont vous avez besoin pour un count à rebours de base dans une table. Parce qu'il ne stocke aucune donnée dans la vue, vous pouvez faire défiler autant que vous le souhaitez:

 @interface Timer : NSObject @property (strong, nonatomic) NSDate *endDate; @property (strong, nonatomic) NSSsortingng *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 = [NSSsortingng ssortingngWithFormat:@"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 = [NSSsortingng ssortingngWithFormat:@"%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 

entrez la description de l'image ici