1 首先创建一个数组,用来创建所有的定时器的时间
- (NSMutableArray *)totalLastTime
{
if (!_totalLastTime) {
_totalLastTime = [NSMutableArray array];
}
return _totalLastTime;
}
2 当从网络请求过来时间之后,循环遍历,行数和时间作为Key,将值作为value放进字典中放进数组
// 所有剩余的时间 lastTime
for (int i = 0; i < strongSelf.bigModel.activeTeamList.count; i ++) {
NSDictionary *dic = strongSelf.bigModel.activeTeamList[i];
NSDate *date = [SKTool changeTimeStringtoDate:dic[@"EndTime"]];
double time = [date timeIntervalSinceDate:[NSDate date]];
NSMutableDictionary *para = [NSMutableDictionary dictionary];
[para setValue:@(i) forKey:@"row"];
[para setValue:[NSNumber numberWithDouble:time] forKey:@"lastTime"];
[self.totalLastTime addObject:para];
}
3 没定时器更新的方法中去取出row 和 时间,更新到cell 上面去,然后再将更新过的时间更新到数组中去。
- (void)refreshLessTime {
NSUInteger time;
for (int i = 0; i < self.totalLastTime.count; i++) {
time = [[[self.totalLastTime objectAtIndex:i] objectForKey:@"lastTime"]integerValue];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[[[self.totalLastTime objectAtIndex:i] objectForKey:@"row"] integerValue] inSection:1];
BaoPinCellA *cell = (BaoPinCellA *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.timeLbL.text = [NSString stringWithFormat:@"剩余:%@",[self lessSecondToDay:--time]];
NSDictionary *dic = @{@"row": [NSString stringWithFormat:@"%li",(long)indexPath.row],@"lastTime": [NSString stringWithFormat:@"%lu",(unsigned long)time]};
[self.totalLastTime replaceObjectAtIndex:i withObject:dic];
}
}