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 > ) {
[[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 是后台计算好的时间差,前端直接使用。
在控制器中直接赋值即可,
如果后台,直接返回的是时间差的话,并且数据可能超过一屏幕
注意,滚动就会出现问题,(每次离开屏幕后,再次进入屏幕的时候,显示的数据就会是开始的数据进行的倒计时),
这时候建议,后台返回到期时间,自己用现在时间计算时间差,
tableViewcell上放定时器的更多相关文章
- tableViewCell上的定时器拖动阻塞
if (_timer == nil) { _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@sele ...
- 一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事
1.一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事
- UITableViewCell上放UICollectionView ,UICollectionViewCell无法复用bug
如题: UITableViewCell上放UICollectionView ,UICollectionViewCell无法复用bug 如果UITableViewCell的size大于整个collect ...
- 获取屏幕上的某个控件相对位置,尤其是tableviewcell上的某一个控件的相对位置
我的需求就是tableviewcell上的按钮,点击就会出现一个弹框: 主要就是获取,所点击的cell上控件的相对位置: CGPoint buttonCenter = CGPointMake(btn. ...
- 机器学习进阶-项目实战-信用卡数字识别 1.cv2.findContour(找出轮廓) 2.cv2.boudingRect(轮廓外接矩阵位置) 3.cv2.threshold(图片二值化操作) 4.cv2.MORPH_TOPHAT(礼帽运算突出线条) 5.cv2.MORPH_CLOSE(闭运算图片内部膨胀) 6. cv2.resize(改变图像大小) 7.cv2.putText(在图片上放上文本)
7. cv2.putText(img, text, loc, text_font, font_scale, color, linestick) # 参数说明:img表示输入图片,text表示需要填写的 ...
- tableview中在tableheaderView上放一个视图,第一次进入视图显示不正常,往下拉视图仍然不正常,往上拉视图正常
解决办法: frame来源不正常,从直接在viewDidLoad方法中设置的frame,改为 - (void)viewDidLayoutSubviews { [super viewDidLayoutS ...
- iOS tableview上放textfield
用UITableViewController就可以了,处理键盘弹出和消失的代码已经封装在UITableViewController里了.
- VMware要不要装在固态SSD上,虚拟机系统文件要不要放固态SSD上,虚拟机伤不伤固态SSD
先说结论:VMware建议装在固态上运行快,系统文件最好放在机械硬盘上(有钱除外),虚拟机会伤固态. 今天在学淘淘商城中的搜索项目,克隆了一个虚拟机,这个虚拟机开机贼慢,令人无法忍受,我觉得我再也受不 ...
- 深入理解定时器系列第一篇——理解setTimeout和setInterval
× 目录 [1]setTimeout [2]setInterval [3]运行机制[4]作用[5]应用 前面的话 很长时间以来,定时器一直是javascript动画的核心技术.但是,关于定时器,人们通 ...
随机推荐
- elasticsearch 聚合查询
1. 按照 tags 字段 进行分组 GET /ecommerce/product/_search{ "size": 0, "aggs": { &quo ...
- 讲完.class,Class之后,继续。
讲完.class,Class之后,继续. 1)泛化的Class引用 Class也可以加入泛型,加入之后会进行类型检查. 贴一下书上原话,Class<?>优于Class,虽然他们是等价的,C ...
- SSM+Druid的搭建
SSM+druid开发配置 工程目录 1.先从pom文件开始吧 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- POJ1027 The Same Game
题目来源:http://poj.org/problem?id=1027 题目大意: 题目说的就是现在蛮流行的手机小游戏popstar,求用贪心方法能得到多少分. 小球有三种颜色:R/G/B.横向.纵向 ...
- linux下ssh key秘钥登陆远程服务器设置
本地的用户名需要和ssh服务器的用户名一致 1.在Server服务器上加载私钥文件ssh-add wang_rsa 2.如果系统提示:could not open a connection to yo ...
- P2062 分队问题(贪心orDP)
题目描述 给定n个选手,将他们分成若干只队伍.其中第i个选手要求自己所属的队伍的人数大等于a[i]人. 在满足所有选手的要求的前提下,最大化队伍的总数. 注:每个选手属于且仅属于一支队伍. 输入输出格 ...
- CF D. Recovering BST (区间DP)
题意:给你n个节点,每个节点有一个权值,两个点可以连边当且仅当这两个点的gcd>1,问你这n个点能否构成一个二叉搜索树(每个节点最多有两个儿子,且左儿子小于右儿子),输入为递增顺序. 分析: 若 ...
- 1088 Rational Arithmetic(20 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- linux读取yaml文件的某个属性值
trigger=$(cat test.yaml | grep "trigger" | awk '{print $2}') 该条命令的意思是:读取test.yaml文件中的trigg ...
- Microsoft JET Database Engine (0x80004005)未指定的错误解决
Microsoft JET Database Engine (0x80004005)未指定的错误,这个错误只有在使用Access数据库时才能出现 出现以上问题,可以使用以下步骤进行解决问题: 1.系统 ...