我是靠谱客的博主 冷酷柚子,这篇文章主要介绍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];

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// // 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中内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部