创建TableView倒计时的简单思路

1.首先controller只负责创建一个NSTimer,并且放入Runloop里。

self.m_timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerEvent) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:_m_timer forMode:NSRunLoopCommonModes];

响应事件timerEvent里需要每隔一秒发送一个通知给cell

- (void)timerEvent

{

    for(intcount =0; count

    {

        TimeModel *model =self.m_dataArray[count];

        [model countDown];

    }

    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TIME_CELL object:nil];

}

2.创建时间Model负责对时间进行减1操作,并且把剩余时间转化成想要的格式赋值给cell。

.h

@property (nonatomic,assign)int  countNum

+ (instancetype)timeModelWithTime:(int)time;

- (void)countDown;

- (NSString*)currentTimeString;

.m
+ (instancetype)timeModelWithTime:(int)time

{

    TimeModel*model = [self new];

    model.countNum= time;

    return model;

}

- (void)countDown

{

    _countNum -= 1;

}

- (NSString*)currentTimeString

{

    if (_countNum <= 0)

    {

        return@"活动结束";

    }

    else

    {

        return [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)_countNum/3600,(long)_countNum%3600/60,(long)_countNum%60];

    }

}

你可能感兴趣的:(创建TableView倒计时的简单思路)