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上放定时器的更多相关文章

  1. tableViewCell上的定时器拖动阻塞

    if (_timer == nil) { _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@sele ...

  2. 一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事

    1.一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事

  3. UITableViewCell上放UICollectionView ,UICollectionViewCell无法复用bug

    如题: UITableViewCell上放UICollectionView ,UICollectionViewCell无法复用bug 如果UITableViewCell的size大于整个collect ...

  4. 获取屏幕上的某个控件相对位置,尤其是tableviewcell上的某一个控件的相对位置

    我的需求就是tableviewcell上的按钮,点击就会出现一个弹框: 主要就是获取,所点击的cell上控件的相对位置: CGPoint buttonCenter = CGPointMake(btn. ...

  5. 机器学习进阶-项目实战-信用卡数字识别 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表示需要填写的 ...

  6. tableview中在tableheaderView上放一个视图,第一次进入视图显示不正常,往下拉视图仍然不正常,往上拉视图正常

    解决办法: frame来源不正常,从直接在viewDidLoad方法中设置的frame,改为 - (void)viewDidLayoutSubviews { [super viewDidLayoutS ...

  7. iOS tableview上放textfield

    用UITableViewController就可以了,处理键盘弹出和消失的代码已经封装在UITableViewController里了.

  8. VMware要不要装在固态SSD上,虚拟机系统文件要不要放固态SSD上,虚拟机伤不伤固态SSD

    先说结论:VMware建议装在固态上运行快,系统文件最好放在机械硬盘上(有钱除外),虚拟机会伤固态. 今天在学淘淘商城中的搜索项目,克隆了一个虚拟机,这个虚拟机开机贼慢,令人无法忍受,我觉得我再也受不 ...

  9. 深入理解定时器系列第一篇——理解setTimeout和setInterval

    × 目录 [1]setTimeout [2]setInterval [3]运行机制[4]作用[5]应用 前面的话 很长时间以来,定时器一直是javascript动画的核心技术.但是,关于定时器,人们通 ...

随机推荐

  1. 反射实现数据库增删改查DAO及DAOImpl源代码(一)

    这是DAO层,第一次尝试,如有bug希望能即使反馈,我们共同进步.具体的用法和实现原理我会在前面几篇博客中补充更新.配置文件及项目目录结构会在下一篇中贴出! package com.javasm.su ...

  2. [Django笔记] models 深入学习

    对着官方文档撸一遍,顺便做点笔记 models 定义了本应用的数据库表结构.底层可以由不同的数据库封装实现,因为不同的数据库字段类型不一样,因此,跟以往直接用单一数据库(如mysql)建立的应用有很大 ...

  3. [Xcode 实际操作]五、使用表格-(7)UITableView单元格间隔背景色

    目录:[Swift]Xcode实际操作 本文将演示如何给表格设置间隔的背景颜色. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先 ...

  4. lynis检测

    https://www.cnblogs.com/ssooking/p/6034402.html Usage: lynis command [options] Command: audit audit ...

  5. C++ | char* 在类中实践笔记

    在C++中,当类中定义有char * 变量时,在传参,构造函数,复制构造函数如何创建及赋值,来一个简单的例子就明了: #include<iostream> #include<stri ...

  6. 原生DOM操作方法小结

    1.0   DOM结构 父节点 兄弟节点 当前节点 属性节点 子节点 兄弟节点 一般任意一个节点我们都会从父节点,子节点,以及兄弟节点的角度去考察.节点一般我们只需关注元素节点,属性节点及文本节点即可 ...

  7. ACM 大神的经验加技巧(当然不是我的拉——

    大神 犯错合集及需要注意的东西 1.在一个地图求最大面积的类问题中,要注意障碍结点的影响. 2.ll(),表示的是在运算后把括号内强制转化为类型ll,而(ll)表示后面的每个玩意都强制转化为类型ll. ...

  8. spring框架详细课程视频

    https://ke.qq.com/course/27346#term_id=100012852

  9. object.wait为什么要和synchronized一块使用

    Object.wait 中JDK提供的doc文档 Causes the current thread to wait until another thread invokes the notify() ...

  10. 源码分析Thread

    多次start ?? IlleageStateException