后台获取的Model数据中时间秒数second存放在字典里,用indexPath做key值
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSDictionary *dic = @{@"indexPath":[NSString stringWithFormat:@"%ld",indexPath.row],@"lastTime": model.second};
[_array addObject:dic];
NSLog(@"----%ld----",_array.count);
//调用定时器
[self startTimer];
#pragma mark 定时器开启
- (void)startTimer
{
if (!self.timer) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshLessTime) userInfo:nil repeats:YES];
//如果不添加下面这条语句,在UITableView拖动的时候,会阻塞定时器的调用
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:UITrackingRunLoopMode];
}
}
- (void)refreshLessTime
{
NSUInteger time;
for (int i = 0; i < _array.count; i++) {
time = [[[_array objectAtIndex:i] objectForKey:@"lastTime"]integerValue];
NSLog(@"%ld",time);
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[[[_array objectAtIndex:i] objectForKey:@"indexPath"] integerValue] inSection:0];
LXOrderingTableViewCell *cell = (LXOrderingTableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
cell.labPersistTime.text = [NSString stringWithFormat:@"%@",[self lessSecondToDay:++time]];
NSDictionary *dic = @{@"indexPath": [NSString stringWithFormat:@"%ld",indexPath.row],@"lastTime": [NSString stringWithFormat:@"%ld",time]};
[_array replaceObjectAtIndex:i withObject:dic];
}
}
- (NSString *)lessSecondToDay:(NSUInteger)seconds
{
//秒化成 时分秒 显示
NSUInteger hour = (NSUInteger)(seconds/3600);
NSUInteger min = (NSUInteger)(seconds%(3600))/60;
NSUInteger second = (NSUInteger)(seconds%60);
NSString *time = [NSString stringWithFormat:@"%lu小时%lu分钟%lu秒",(unsigned long)hour,(unsigned long)min,(unsigned long)second];
return time;
}