自己稍微记录一下,方便以后用到:

先创建一个定时器的类:

#import "TimeCenter.h"
@interface TimeCenter ()
@property (nonatomic, assign) NSInteger timeInterval;// 累计时间
@property (nonatomic, strong) NSTimer *timer; @end
@implementation TimeCenter
+ (instancetype)shareCenter {
static TimeCenter *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[TimeCenter alloc]init];
}); return center;
}
//启动
- (void)start {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerEvent) userInfo:nil repeats:YES];
self.timer=timer;
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
//停止
- (void)stop
{
if (_timer != nil) {
[self.timer invalidate];
}
} //事件
- (void)timerEvent
{
if (_timer != nil) {
self.timeInterval++;
}
}

在控制器创建的时候开始定时器,

然后在cell中添加kvo 监听事件:

@implementation TableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
static NSString *ID = @"tableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
return cell;
} - (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//第二种:KVO监听
[[TimeCenter shareCenter] addObserver:self
forKeyPath:@"timeInterval"
options:NSKeyValueObservingOptionOld
|NSKeyValueObservingOptionNew
context:nil];
}
return self;
} - (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context{
if ([keyPath isEqual:@"timeInterval"]) {
[self reloadCell];
}
} - (void)setTimeModel:(TimeModel *)timeModel{
_timeModel=timeModel;
[self reloadCell];
} - (void)reloadCell{
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
NSInteger time = [date timeIntervalSince1970];
NSInteger leftTime = [self.timeModel.time integerValue] - time;
if (leftTime <0){
self.textLabel.text =@"已结束";
}else{
self.textLabel.text = [NSString stringWithFormat:@"剩余%ld天%02ld:%02ld:%02ld", leftTime/(60*60*24),
(leftTime/3600)%24,
(leftTime/60)%60,
leftTime%60];
}
}
- (void)dealloc
{
[[TimeCenter shareCenter] removeObserver:self
forKeyPath:@"timeInterval"];

利用kvo实现列表倒计时的更多相关文章

  1. Android利用RecyclerView实现列表倒计时效果

    最近面试时,面试官问了一个列表倒计时效果如何实现,然后脑袋突然懵的了O(∩_∩)O,现在记录一下. 运行效果图 实现思路 实现方法主要有两个: 1.为每个开始倒计时的item启动一个定时器,再做更新i ...

  2. python利用or在列表解析中调用多个函数.py

    python利用or在列表解析中调用多个函数.py """ python利用or在列表解析中调用多个函数.py 2016年3月15日 05:08:42 codegay & ...

  3. 利用echo命令实现倒计时的功能

    echo -e:启用反斜线控制字符的转换        -E:关闭反斜线控制字符的转换(预设如此)        -n:取消行末之换行符号(与 -e 选项下的 \c 字符同意 -e参数下的控制参数 \ ...

  4. java翻译到mono C#实现系列(4) 利用CountDownTimer类实现倒计时功能 mono版

    群里的朋友问利用CountDownTimer类实现倒计时功能怎么实现,我就百度了,参考http://blog.csdn.net/qq344429461/article/details/7521361写 ...

  5. 利用kvo对集合进行操作

    利用kvo对集合进行操作 NSLog(@"其他学生的成绩%@", [array valueForKeyPath:@"point"]); NSLog(@" ...

  6. python3之利用字典和列表实现城市多级菜单

    利用字典和列表实现城市多级菜单 #coding:utf-8 #利用字典和列表实现城市多级菜单 addrIndex = {":"福建"} addrDict = {" ...

  7. C利用可变参数列表统计一组数的平均值,利用函数形式参数栈原理实现指针运算

    //描述:利用可变参数列表统计一组数的平均值 #include <stdarg.h> #include <stdio.h> float average(int num, ... ...

  8. 微信小程序 列表倒计时

    最近要实现一个列表倒计时的功能,写了个demo 展示图 <view class="center colu"> <view class="time&quo ...

  9. 利用UICollectionView实现列表和宫格视图的切换

    很多时候我们需要列表和宫格视图的来回切换,就像苹果的天气应用一样,我之前见过一个用tableview和collectionview来实现这种效果的,我本人不太喜欢这个,那么有没有更好的方法呢?答案是: ...

随机推荐

  1. Mysql的子查询与连接查询

    子查询: 在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句 主查询和子查询的关系: 子查询是嵌入到主查询中,子查询是辅助主查询的,要 ...

  2. JMeter - 如何在多个测试环境中运行多个线程组

    概述: 作为性能测试的一部分,我不得不为我们的应用程序提供各种用例/业务工作流程的性能测试脚本.当我设计我的性能测试脚本时,我将确保我有本文中提到的可重用测试脚本. JMeter - 如何创建可重用和 ...

  3. 找出list中的不同元素、删除两个list中相同的对象

    package com.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; ...

  4. CentOS6.5安装MySQL5.7(也适合其他版本安装)

     1.查看是否已经安装过mysql或其依赖,若已装过要先将其删除,否则第4步使用yum安装时会报错: 方法一:yum list installed | grep mysql 方法二:rpm -qa | ...

  5. 分层图 (可以选择K条路的权为0,求最短路)

    分层图可以处理从图中选取k条边使其边权变为0,求最短路 Description 在你的强力援助下,PCY 成功完成了之前的所有任务,他觉得,现在正是出去浪的大好时光.于是,他来到高速公路上,找到一辆摩 ...

  6. jmeter使用BeanShell Sampler测试自己写的java接口(一)

    上次直接使用jmeter里面的FTPsampler没有连接成功 现在想着自己写java代码,通过jmeter进行调用进行连接测试实现并发 代码引文: http://www.cnblogs.com/ch ...

  7. Selenium----Selenium WebDriver /RC工作原理

    1.Selenium RC 工作原理 说明:客户端库文件将命令传递给server.接着server使用selenium-core的javaScript命令传递给浏览器,浏览器会使用自带的javaScr ...

  8. leetcode 91. 解码方法

    题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数 ...

  9. Gym - 100221D 一题一直没过的dfs,,应该是纯手动码?

    不写了,10年以后再回来写. http://codeforces.com/gym/100221/attachments H题

  10. 三,JVM 自带命令行工具之JMap

    jmap:java内存映像工具 jmap(Memory Map for java ) 命令用于生成堆转储快照(一般被称为headdump 或dump文件) jmap命令格式:jmap [option ...