tableViewcell上放定时器
2024-09-01 11:21:18
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 > ) {
[[TimerManger shareTimer] startTimerWithTimeInterVal:];
}
} - (void)TimerSelector
{
_diffTime = _diffTime -;
self.textLabel.text = [NSString stringWithFormat:@"倒计时:%@",[self gettimestrWithdifftime:_diffTime]];
if (_diffTime <= ) {
self.textLabel.text = @"倒计时:00:00:00";
}
} - (NSString *)gettimestrWithdifftime:(double)difftime
{
//天数
NSString *days = [NSString stringWithFormat:@"%02ld", (NSInteger)((difftime)////)];
//小时数
NSString *hours = [NSString stringWithFormat:@"%02ld", (NSInteger)((difftime)///)%];
//分钟数
NSString *minute = [NSString stringWithFormat:@"%02ld", (NSInteger)((difftime)//)%];
//秒数
NSString *second = [NSString stringWithFormat:@"%02ld", ((NSInteger)(difftime))/%]; return [NSString stringWithFormat:@"%@天%@小时%@分%@",days,hours,minute,second];
}
@end
diffTimestr 是后台计算好的时间差,前端直接使用。
在控制器中直接赋值即可,
如果后台,直接返回的是时间差的话,并且数据可能超过一屏幕
注意,滚动就会出现问题,(每次离开屏幕后,再次进入屏幕的时候,显示的数据就会是开始的数据进行的倒计时),
这时候建议,后台返回到期时间,自己用现在时间计算时间差,
最新文章
- ASP.NET Core 在 JSON 文件中配置依赖注入
- TFS 2015(Visual Studio Team Foundation Server)的下载和安装
- CF memsql Start[c]UP 2.0 A
- jquery分页插件
- SQL*Loader之CASE4
- JavaSE_ Java基础 总目录(1~6)
- 在SSH整合框架中经常会用到Service层,请问大家这个Service层有什么具体的作用,可不可以不用这个Service层呢?
- Delphi中使用python脚本读取Excel数据
- 【Python】 docker-py 用Python调用Docker接口
- SQLServer之创建DML AFTER INSERT触发器
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) A,B,C
- MySQL半同步安装以及参数
- Linux非常有用的命令
- Java Web开发和Python Web开发之间的区别
- JNI探秘-----FileInputStream的read方法详解
- 2017年Java面试题整理
- 如何设置nginx日志格式来查看负载分担结果
- JSTL详解(二)
- 『AngularJS』一点小小的理解
- Python的输入输出
热门文章
- python 基于 wordcloud + jieba + matplotlib 生成词云
- 安居客scrapy房产信息爬取到数据可视化(下)-可视化代码
- 运算符优先级 (JavaScript)
- c# Equals对比忽略大小写
- Appium+Python入门学习总结
- UVALive 7500 Boxes and Balls 2015EC final 签到题 二分
- java编程如何实现2017-01-16 22:28:26.0这样的时间数据,转换成2017:01:16:22:28:26这样的时间数据
- 解决Hadoop无法加载本地库的问题: Unable to load native-hadoop library for your platform
- IDEA使用汇总
- IE浏览器与非IE浏览器JS日期兼容性问题处理