iOS大咖说iosiOS学习笔记

iOS - 关于每个cell上都出现倒计场景的的研究

2017-06-30  本文已影响1212人  鲲鹏DP

背景

以前就有人问过这样一个问题:如果一个tableView的很多或者所有cell上都显示一个倒计时,该怎么实现? 今天自己恰好也遇到了这样的需求:很多产品,每个都有一个时限,在时限内才可以申购,过了申购功能就会关闭.简单描述就是,每个cell上有个倒计时,时间结束与否,点击cell响应的事件是不一样的.那么怎么实现呢?下面谈谈自己的思考过程.


1.Cell内部加一个定时器

- (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];
}
cel内部定时器.png
- (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];
    }
}

2. 在tableView的parentView中实现

/**定时器触发*/
- (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. 值得注意的几个地方

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

4. 后记:由于这只是一个最初的Demo,也只是一些个人的初步看法,难免有些疏漏,如有纰漏,还望指正.

上一篇下一篇

猜你喜欢

热点阅读