我是靠谱客的博主 冷酷柚子,最近开发中收集的这篇文章主要介绍Cell中的倒计时(NSTimer),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Cocoa中的预定义模式有

  • UITrackingRunLoopMode:例如,当手指按住UITableView拖动时就会处于此模式

  • NSRunLoopCommonModes:在Cocoa应用程序中,默认情况下Common Modes包含default modes,event Tracking modes.

  • NSTimer在tableview滑动时失效解决方法


  • [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];


  • [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

//
//  CountDownInCellViewController.m
//  Cell中倒计时
//

#import "CountDownInCellViewController.h"
#import "CountDownCell.h"

@interface CountDownInCellViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *table;
@property (weak,nonatomic) NSTimer *timer;
@property (strong,nonatomic) NSArray *dataSource;

@end

@implementation CountDownInCellViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"cell中倒计时";
    // 测试数据
    self.dataSource = @[@"2019-09-22 19:26:02",@"2019-09-22 19:12:02",
                        @"2019-09-22 19:35:02",@"2019-09-22 19:14:02",
                        @"2019-09-22 19:33:02",@"2019-09-22 19:26:02",
                        @"2019-09-22 15:26:02",@"2019-09-22 19:26:55",
                        @"2019-09-22 19:26:02",@"2019-09-22 19:12:02",
                        @"2019-09-22 19:35:02",@"2019-09-22 19:14:02",
                        @"2019-09-22 19:33:02",@"2019-09-22 19:26:02",
                        @"2019-09-22 15:26:02",@"2019-09-22 19:26:55"];
    
    [self accessToken];
    
    [_table registerNib:[UINib nibWithNibName:@"CountDownCell" bundle:nil] forCellReuseIdentifier:@"CountDownCell"];
    
    _table.tableFooterView = [UIView new];
    _table.rowHeight = 100;
    
}
#pragma mark - 转换时间
-(NSString *)getNowTimeWithString:(NSString *)aTimeString{
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    // 截止时间date格式
    NSDate  *expireDate = [formater dateFromString:aTimeString];
    NSDate  *nowDate = [NSDate date];
    // 当前时间字符串格式
    NSString *nowDateStr = [formater stringFromDate:nowDate];
    // 当前时间date格式
    nowDate = [formater dateFromString:nowDateStr];
    
    NSTimeInterval timeInterval =[expireDate timeIntervalSinceDate:nowDate];
    
    int days = (int)(timeInterval/(3600*24));
    int hours = (int)((timeInterval-days*24*3600)/3600);
    int minutes = (int)(timeInterval-days*24*3600-hours*3600)/60;
    int seconds = timeInterval-days*24*3600-hours*3600-minutes*60;
    
    NSString *dayStr;NSString *hoursStr;NSString *minutesStr;NSString *secondsStr;
    //天
    dayStr = [NSString stringWithFormat:@"%d",days];
    //小时
    hoursStr = [NSString stringWithFormat:@"%d",hours];
    //分钟
    if(minutes<10)
        minutesStr = [NSString stringWithFormat:@"0%d",minutes];
    else
        minutesStr = [NSString stringWithFormat:@"%d",minutes];
    //秒
    if(seconds < 10)
        secondsStr = [NSString stringWithFormat:@"0%d", seconds];
    else
        secondsStr = [NSString stringWithFormat:@"%d",seconds];
    if (hours<=0&&minutes<=0&&seconds<=0) {
        return @"活动已经结束!";
    }
    if (days) {
        return [NSString stringWithFormat:@"%@天 %@小时 %@分 %@秒", dayStr,hoursStr, minutesStr,secondsStr];
    }
    return [NSString stringWithFormat:@"%@小时 %@分 %@秒",hoursStr , minutesStr,secondsStr];
}

-(void)accessToken {
    if (!self.timer) {
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                      target:self
                                                    selector:@selector(updateTime)
                                                    userInfo:nil
                                                     repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
    
}

-(void)updateTime
{
    NSLog(@"timer runing ~~~~");
    NSArray  *cells = self.table.visibleCells; //取出屏幕可见ceLl
    for (CountDownCell *cell in cells) {
        cell.countDownLabel.text = [self getNowTimeWithString:self.dataSource[cell.tag]];
        if ([cell.countDownLabel.text isEqualToString:@"活动已经结束!"]) {
            cell.countDownLabel.textColor = [UIColor redColor];
        }else{
            cell.countDownLabel.textColor = [UIColor orangeColor];
        }
    }
    // 或者下面的方法
//    for (int i = 0; i < self.dataSource.count; i++) {
//
//        NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
//        CountDownCell*cell = [self.table cellForRowAtIndexPath:indexPath];
//        if (cell) {
//            cell.countDownLabel.text = [self getNowTimeWithString:self.dataSource[cell.tag]];
//            if ([cell.countDownLabel.text isEqualToString:@"活动已经结束!"]) {
//                cell.countDownLabel.textColor = [UIColor redColor];
//            }else{
//                cell.countDownLabel.textColor = [UIColor orangeColor];
//            }
//        }
//    }
    
}

#pragma mark - tableview代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CountDownCell *cell = (CountDownCell *)[tableView dequeueReusableCellWithIdentifier:@"CountDownCell"];
    cell.timeLabel.text = [NSString stringWithFormat:@"活动 %@ 结束",self.dataSource[indexPath.row]];
    cell.timeLabel.adjustsFontSizeToFitWidth = YES;
    cell.countDownLabel.text = [self getNowTimeWithString:self.dataSource[indexPath.row]];
    if ([cell.countDownLabel.text isEqualToString:@"活动已经结束!"]) {
        cell.countDownLabel.textColor = [UIColor redColor];
    }else{
        cell.countDownLabel.textColor = [UIColor orangeColor];
    }
    cell.tag = indexPath.row;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.timer invalidate];
}

#pragma mark - 验证是否释放
-(void)dealloc{
    
    NSLog(@"%s dealloc",object_getClassName(self));
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

最后

以上就是冷酷柚子为你收集整理的Cell中的倒计时(NSTimer)的全部内容,希望文章能够帮你解决Cell中的倒计时(NSTimer)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部