tableViewcell上放定时器

tableviewcell上的定时器:

1.创建一个管理定时器的TimerManger类,

TimerManger.h

#import <Foundation/Foundation.h>

@interface TimerManger : NSObject

/**
 结束定时器
 */
- (void)stopTimer;

/**
 开始定时器
 */
- (void)startTimerWithTimeInterVal:(NSTimeInterval)timeInterVal;

/**
 单利
 */
+ (instancetype)shareTimer;

@end

TimerManger.m

#import "TimerManger.h"

static NSString * TIMER_NOTIFICATION = @"TIMER_NOTIFICATION";

@interface TimerManger ()
/**
 定时器
 */
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation TimerManger

+ (instancetype)shareTimer
{
    static dispatch_once_t onceToken;
    static TimerManger *instance;
    dispatch_once(&onceToken, ^{
        instance = [[TimerManger alloc]init];
    });
    return instance;
}

- (void)startTimerWithTimeInterVal:(NSTimeInterval)timeInterVal
{
    if (_timer) return;
    _timer = [NSTimer timerWithTimeInterval:timeInterVal
                                     target:self
                                   selector:@selector(timerAction:)
                                   userInfo:nil
                                    repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer
                              forMode:NSRunLoopCommonModes];
}
/**
 定时器调用事件
 
 @param timer timer
 */
- (void)timerAction:(NSTimer *)timer
{
    [[NSNotificationCenter defaultCenter] postNotificationName:TIMER_NOTIFICATION
                                             object:nil
                                           userInfo:nil];
}

/**
 结束定时器
 */
- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

@end

tableViewcell中的代码

tableviewcell.h

#import <UIKit/UIKit.h>

@interface TimerTableViewCell : UITableViewCell

/**
 时间差
 */
@property (nonatomic, copy) NSString * diffTimestr;

@end

tableviewcell.m

#import "TimerTableViewCell.h"
#import "TimerManger.h"

@interface TimerTableViewCell ()

@property (nonatomic, assign) double diffTime;
@end

@implementation TimerTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TimerSelector) name:@"TIMER_NOTIFICATION" object:nil];
    }
    return self;
}

- (void)setDiffTimestr:(NSString *)diffTimestr
{
    _diffTime = [diffTimestr doubleValue];
        if (_diffTime > 0 ) {
        [[TimerManger shareTimer] startTimerWithTimeInterVal:1];
    }
}

- (void)TimerSelector
{
    _diffTime = _diffTime -1000;
    self.textLabel.text = [NSString stringWithFormat:@"倒计时:%@",[self gettimestrWithdifftime:_diffTime]];
    if (_diffTime <= 0) {
        self.textLabel.text = @"倒计时:00:00:00";
    }
}

- (NSString *)gettimestrWithdifftime:(double)difftime
{
    //天数
    NSString *days = [NSString stringWithFormat:@"%02ld", (NSInteger)((difftime)/1000/60/60/24)];
    //小时数
    NSString *hours = [NSString stringWithFormat:@"%02ld", (NSInteger)((difftime)/1000/60/60)%24];
    //分钟数
    NSString *minute = [NSString stringWithFormat:@"%02ld", (NSInteger)((difftime)/1000/60)%60];
    //秒数
        NSString *second = [NSString stringWithFormat:@"%02ld", ((NSInteger)(difftime))/1000%60];
    
    return [NSString stringWithFormat:@"%@天%@小时%@分%@",days,hours,minute,second];
}
@end
diffTimestr 是后台计算好的时间差,前端直接使用。

在控制器中直接赋值即可,

如果后台,直接返回的是时间差的话,并且数据可能超过一屏幕

注意,滚动就会出现问题,(每次离开屏幕后,再次进入屏幕的时候,显示的数据就会是开始的数据进行的倒计时),

这时候建议,后台返回到期时间,自己用现在时间计算时间差,

原文地址:https://www.cnblogs.com/liuwenqiang/p/6883177.html