ios 怎么样tableview 里面加一个倒计时
发布网友
发布时间:2022-05-01 15:55
我来回答
共1个回答
热心网友
时间:2022-06-19 03:01
最近发得帖,都是自己来结的 再结一次。希望对有遇到同样问题的人有帮助,我相信,分享才有进步。
//在服务器取到数据后调用
-(void)initOverTime :(NSMutableArray *)dataInfo
{
for (int i=0; i<dataInfo.count; i++) {
NSDictionary *dic=[dataInfo objectAtIndex:i];
NSUInteger overSec= [self getOverTime:dic];
NSMutableDictionary *overDic=[[NSMutableDictionary alloc]init];
[overDic setValue:[NSString stringWithFormat:@"%d",i] forKey:@"indexPath"];
[overDic setValue:[NSString stringWithFormat:@"%d",overSec] forKey:@"lastTime"];
[totalLastTime addObject:overDic];
}
[self startTimer];
}
-(NSUInteger)getOverTime :(NSDictionary *)dataInfo
{
NSString*timeFormat=@"yyyy-MM-dd hh:mm:ss";
NSDate *systemDate=getShortDateFromString(@"yyyy-MM-dd HH:mm:ss", systemTime);
NSDate *startDate=getShortDateFromString(timeFormat, [dataInfo objectForKey:@"startTime"]);
NSDate *endDate=getShortDateFromString(timeFormat, [dataInfo objectForKey:@"endTime"]);
NSUInteger systemInt=[systemDate timeIntervalSince1970];
NSUInteger startInt=[startDate timeIntervalSince1970];
NSUInteger endInt=[endDate timeIntervalSince1970];
NSInteger resie;
if (startInt>systemInt) {
resie=startInt-systemInt; //开始时间大于现在的时间,团购未开始
}else
{
resie=endInt-systemInt;//团购开始,还能多少时间结束
}
return resie;
}
- (void)startTimer
{
NSTimer *timer = [NSTimer scheledTimerWithTimeInterval:1 target:self selector:@selector(refreshLessTime) userInfo:nil repeats:YES];
// 如果不添加下面这条语句,在UITableView拖动的时候,会阻塞定时器的调用
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
}
- (void)refreshLessTime
{
NSUInteger time;
for (int i = 0; i < totalLastTime.count; i++) {
time = [[[totalLastTime objectAtIndex:i] objectForKey:@"lastTime"] integerValue];
// NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:[[[totalLastTime objectAtIndex:i] objectForKey:@"indexPath"] integerValue]];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[[[totalLastTime objectAtIndex:i] objectForKey:@"indexPath"] integerValue] inSection:0];
GroupListCell *cell = (GroupListCell *)[tbView cellForRowAtIndexPath:indexPath];
cell.timeLabel.text = [NSString stringWithFormat:@"剩%@",[self lessSecondToDay:--time]];
NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
[dic setValue:[NSString stringWithFormat:@"%d",i] forKey:@"indexPath"];
[dic setValue:[NSString stringWithFormat:@"%d",time] forKey:@"lastTime"];
[totalLastTime replaceObjectAtIndex:i withObject:dic];
}
}
- (NSString *)lessSecondToDay:(NSUInteger)seconds
{
NSUInteger day = (NSUInteger)seconds/(24*3600);
NSUInteger hour = (NSUInteger)(seconds%(24*3600))/3600;
NSUInteger min = (NSUInteger)(seconds%(3600))/60;
NSUInteger second = (NSUInteger)(seconds%60);
NSString *time = [NSString stringWithFormat:@"%lu天%lu小时%lu分%lu秒",(unsigned long)day,(unsigned long)hour,(unsigned long)min,(unsigned long)second];
return time;
}