不同cell有不同的計時器

//所有剩余時間數(shù)組

NSMutableArray *totalLastTime;

在網(wǎng)絡(luò)請求到的所有數(shù)據(jù)中,根據(jù)需要將其中要進行倒計時顯示的數(shù)據(jù)中的剩余時間單獨保存出來,如果這里是所有的數(shù)據(jù)都需要倒計時,則只需要保存時間即可,如果是有部分數(shù)據(jù)才需要倒計時,則可以保存字典,兩個鍵值對分別為其在UITableView的indexPath和剩余時間:num默認從0開始

NSDictionary *dic = @{@"indexPath":[NSStrin stringWithFormat:@"%i",num],@"lastTime": order.payLastTime};

[totalLastTime addObject:dic];

開啟定時器方法:

- (void)startTimer{

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshLessTime) userInfo:@"" repeats:YES];

如果不添加下面這條語句,在UITableView拖動的時候,會阻塞定時器的調(diào)用

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

}

主要的定時器中的方法,在該方法中,遍歷totalLastTime,取出其中保存的lasttime和indexpath,time用來顯示,在顯示完后自減,indexpath代表對應(yīng)顯示的位置,在一次循環(huán)后,將新的time和沒有改變的indexpath從新替換totalLastTime 中對應(yīng)位置的元素,以此保證每一秒執(zhí)行時,顯示time都是最新的。

- (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]];

WLServiceOrderTableViewCell *cell = (WLServiceOrderTableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];

cell.remainingTimeLabel.text = [NSString stringWithFormat:@"剩余支付時間:%@",[self lessSecondToDay:--time]];

NSDictionary *dic = @{@"indexPath": [NSString stringWithFormat:@"%i",indexPath.section],@"lastTime": [NSString stringWithFormat:@"%i",time]};

[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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容