最近項目做了微信支付笔宿,需求是待支付的訂單有效期是24小時之內(nèi),所以在訂單列表可能存在多個訂單的倒計時時間,雖然對大牛來說很簡單计福,但是下面還是記錄下我的實現(xiàn)方法和所遇到的一些使用NSTimer的注意點吧娃磺。
實現(xiàn)方法:
首先我是開一個timer管理多個倒計時薄湿,因為我看過一個倒計時開一個timer的,這樣開多個timer不利于性能豌鸡。接口傳一個最新剩余倒計時毫秒數(shù)就行嘿般,注意是毫秒數(shù)哦,因為一下代碼計算時間單位用的毫秒涯冠。
1.1聲明一個timer屬性
@property (nonatomic, strong) NSTimer *timer;
1.2建立一個時間模型
@interface ZHBTimeLessModel : NSObject
@property (nonatomic,strong) NSString *indexPath;//數(shù)組下標(biāo)(需要倒計時的Cell IndexPath 方便取到相應(yīng)的Cell 改變倒計時時間)
@property (nonatomic,strong) NSString *lastTime;//剩余時間
@property (nonatomic,strong) NSString *orderCount;//訂單中商品數(shù)量
@end
1.3請求到數(shù)據(jù)后將倒計時模型加入數(shù)組
for (int i = 0; i < self.dataArray.count; i++)
{
ZHBMyOderModel *orderM = self.dataArray[i];
ZHBTimeLessModel *model = [[ZHBTimeLessModel alloc] init];
model.indexPath = [NSString stringWithFormat:@"%d",i];
model.lastTime = orderM.restSecond;
model.orderCount = [NSString stringWithFormat:@"%lu",(unsigned long)orderM.details.count]炉奴;
[self.totalLastTime addObject:model];
}
1.4將毫秒轉(zhuǎn)化為時分方法
- (NSString *)lessSecondToDay:(NSUInteger)seconds
{
NSUInteger hour = seconds/60000/60;
NSUInteger min = (seconds/60000)%60;
NSUInteger second = (seconds%60000)/1000;
NSString *time = [NSString stringWithFormat:@"%lu小時%lu分鐘%lu秒",(unsigned long)hour,(unsigned long)min,(unsigned long)second];
return time;
}
1.5刷新時間方法
- (void)refreshLessTime
{
int time;
for (int i = 0; i < self.totalLastTime.count; i++) {
ZHBTimeLessModel *model = self.totalLastTime[i];
time = model.lastTime.intValue;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:[model.indexPath integerValue]];
if (model.orderCount.intValue == 1)
{
ZHBMineOrderCell *cell = (ZHBMineOrderCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if (time <= 1000) {
time -= 1000;
cell.payLabel.text = [NSString stringWithFormat:@"結(jié)束了:%@",[self lessSecondToDay:time]];
[self getHeaderOrderListData];
continue;
}else{
time -= 1000;
cell.payTime.text = [NSString stringWithFormat:@"%@",[self lessSecondToDay:time]];
}
}
//多個訂單
else
{
ZHBMutiOrdersCell *cell = (ZHBMutiOrdersCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if (time <= 1000) {
time -= 1000;
cell.payLabel.text = [NSString stringWithFormat:@"結(jié)束了:%@",[self lessSecondToDay:time]];
[self getHeaderOrderListData];
continue;
}else{
time -= 1000;
cell.payTime.text = [NSString stringWithFormat:@"%@",[self lessSecondToDay:time]];
}
}
NSDictionary *dic = @{@"indexPath": [NSString stringWithFormat:@"%li",(long)indexPath.section],@"lastTime": [NSString stringWithFormat:@"%lu",(unsigned long)time]};
ZHBTimeLessModel *timeModel = [[ZHBTimeLessModel alloc] init];
timeModel.indexPath = [dic objectForKey:@"indexPath"];
timeModel.lastTime = [dic objectForKey:@"lastTime"];
timeModel.orderCount = model.orderCount;
[self.totalLastTime replaceObjectAtIndex:i withObject:timeModel];
}
}
這里注意一下 [self.totalLastTime replaceObjectAtIndex:i withObject:timeModel];
每次都將新的時間代替之前的時間。 還有就是時間模型里的IndexPath下標(biāo)蛇更,這里巧妙的記住了需要倒計時的tabelViewCell下標(biāo)瞻赶,這樣就取出了需要實時更改時間的cell中的Label來改變文字。當(dāng)然也要記住倒計時為0時刷新下網(wǎng)絡(luò)請求來滿足業(yè)務(wù)需求派任。
1.6timer的開啟和關(guān)閉
- (void)viewDidLoad
{
[super viewDidLoad];
[self startTimer];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.timer == nil) {
[self startTimer];
}
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
//取消定時器
[self.timer invalidate];
self.timer = nil;
}
核心代碼就是這些砸逊,下面來說下我使用NSTimer遇到的坑吧。
2.1雖然自己都知道開啟NSTimer掌逛,要記得在不需要用的時候把他關(guān)閉师逸,但是這次我真的忘記關(guān)了,某一次發(fā)現(xiàn)退出頁面timer的數(shù)據(jù)請求還在一直刷新豆混。
2.2在哪開啟和關(guān)閉NSTimer篓像。之前寫在viewDidLoad,當(dāng)從需要倒計時頁面push到下一個頁面的時候在viewDidDisappear關(guān)閉了皿伺,再返回timer就關(guān)了员辩,所以需要在viewWillAppear開啟,但是為了防止開啟多個timer導(dǎo)致倒計時速度加快所以要做一個是否nil的判斷鸵鸥。如果在viewWillDisappear方法里關(guān)閉的話奠滑,就會出現(xiàn)側(cè)滑到一半又停留在倒計時頁面時,timer被關(guān)閉了。
2.3還有就是一點cell重用導(dǎo)致的問題宋税。
暫時總結(jié)這么多了摊崭。