温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS中如何让多个cell上都出现倒计时

发布时间:2021-08-04 11:30:11 来源:亿速云 阅读:154 作者:小新 栏目:移动开发

这篇文章将为大家详细讲解有关iOS中如何让多个cell上都出现倒计时,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

1.Cell内部加一个定时器

既然每个cell都有一个倒计时,时间还可能不一样.根据"高内聚,低耦合"的思想,我首先想着直接让cell自己来实现倒计时功能:每个cell添加一个NSTimer,没隔1秒,让其显示的时间减少一秒.

- (void)timeChange {
 self.totalSeconds --;
 if (self.totalSeconds < 0) {
   self.timerLabel.text = @"倒计时结束";
  return;
 }
 self.timerLabel.text = [self timeChangeWithSeconds:self.totalSeconds];
}
- (void)setDataDict:(NSDictionary *)dataDict {
 _dataDict = dataDict;
 NSString *totalTime = dataDict[@"totalTime"];
 self.totalSeconds = totalTime.integerValue;
 self.timerLabel.text = [self timeChangeWithSeconds:self.totalSeconds];
 if (!_timer) {
  _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
  [[NSRunLoop currentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode];
 }
}
- (NSString*)timeChangeWithSeconds:(NSInteger)seconds {
 NSInteger temp1 = seconds/60;
 NSInteger temp2 = temp1/ 60;
 NSInteger d = temp2 / 24;
 NSInteger h = temp2 % 24;
 NSInteger m = temp1 % 60;
 NSInteger s = seconds %60;
 NSString * hour = h< 9 ? [NSString stringWithFormat:@"0%ld",(long)h] :[NSString stringWithFormat:@"%ld",(long)h];
 NSString *day = d < 9 ? [NSString stringWithFormat:@"0%ld",(long)d] : [NSString stringWithFormat:@"%ld",(long)d];
 NSString *minite = m < 9 ? [NSString stringWithFormat:@"0%ld",(long)m] : [NSString stringWithFormat:@"%ld",(long)m];
 NSString *second = s < 9 ? [NSString stringWithFormat:@"0%ld",(long)s] : [NSString stringWithFormat:@"%ld",(long)s];
 return [NSString stringWithFormat:@"%@天:%@时:%@分:%@秒",day,hour,minite,second];
}

iOS中如何让多个cell上都出现倒计时

乍看,好像一切都OK,但是当我们拖动cell时,会发现一旦cell移除屏幕,再拖回来的时候,又会重头倒计时.当然,这和我在setDataDict:方法中的赋值方式有关,可以通过totalSeconds这个属性,保存当前剩余的时间,下一次再进来的时候,去取保存好的值.

- (void)setDataDict:(NSDictionary *)dataDict {
 _dataDict = dataDict;
  if (self.totalSeconds !=0) {
   self.timerLabel.text = [self timeChangeWithSeconds:self.totalSeconds];
 }else {
  NSString *totalTime = dataDict[@"totalTime"];
  self.totalSeconds = totalTime.integerValue;
  self.timerLabel.text = [self timeChangeWithSeconds:self.totalSeconds];
 }
  if (!_timer) {
  _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
  [[NSRunLoop currentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode];
 }
}

这样做,会发现当cell移除屏幕,再移回来的时候,不再是从头倒计时,但是多拖动几次又会发现新的问题:显示错乱,某个cell出现在了不该出现的位置.

仔细分析不难发现,cell的复用机制是引起上述现象的"罪魁祸首",要解决这个问题,可以让cell不复用,比方说,可以给每个cell绑定不同的标识,达到不复用的目的,看到这里,如果你也是这么想的,那么最好打住,因为为了达到这个目的,而让cell不复用,以牺牲内存占用为代价,无疑是饮鸩止渴,丢了西瓜,捡个芝麻.

值得的注意的是,如果在cell中实现,每个cell都添加一个定时器,这也是一笔可观的开销.

2. 在tableView的parentView中实现

既然在cell中实现遇到的坑比较多,那么又想着在外面做.这样有一个明显的好处,就是只需要一个定时器.每次触发,让每个cell上显示的时间递减.由于tableView的显示,取决于传入的数据,只要我在传入数据之前把需要传的数据处理好,这样就不会因为cell的复用机制而带来显示错乱的问题.运行代码,发现问题圆满解决!

/**定时器触发*/
- (void)timeChange {
 NSMutableArray *tempArrM = [NSMutableArray array];
 for (NSDictionary *dict in self.dataArr) {
  NSString *totalTime = dict[@"totalTime"];
  if ([totalTime isEqualToString:@"0"]) {
    totalTime = @"0";
  }else {
   totalTime = [NSString stringWithFormat:@"%ld",totalTime.integerValue -1];
  }
  [tempArrM addObject:@{@"totalTime":totalTime}];
 }
 self.dataArr = tempArrM;
 [self.pageTableView reloadData];
}

3. 值得注意的几个地方

当我们拖动cell时,如果发现定时器不工作,可以用如下方式解决.

 _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
   [[NSRunLoop currentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode];

关于数据的传入:

  • 直接提供产品到目前为止还剩多少时间.每个产品对应一个总的时间,用于倒计时.那么,后台给我提供时间时就可以把每个产品对应的总时间返给我们.但是,这样就要求后台自己实时去计算每个产品在我们请求数据时还剩多少时间.

  • 后台把每种产品的截止时间和当前的系统时间返给我们.系统时间,我们可用于矫正自己的系统时间(APP显示的时间是可以人为修改的,并且不通设备之间,iOS与Android之间的时间有可能存在差异,为了统一所以需要矫正),通过矫正好的时间和截止时间,我们就能知道,该产品还剩多少时间.

虽然,我这边自己用的第一种方式写的Demo,但是,相比之下,我更加倾向于第二种数据的传递方式,准确性高,也能为后端同事剩些事.

关于“iOS中如何让多个cell上都出现倒计时”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI