利用kvo实现列表倒计时
自己稍微记录一下,方便以后用到:
先创建一个定时器的类:
#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实现列表倒计时的更多相关文章
- Android利用RecyclerView实现列表倒计时效果
最近面试时,面试官问了一个列表倒计时效果如何实现,然后脑袋突然懵的了O(∩_∩)O,现在记录一下. 运行效果图 实现思路 实现方法主要有两个: 1.为每个开始倒计时的item启动一个定时器,再做更新i ...
- python利用or在列表解析中调用多个函数.py
python利用or在列表解析中调用多个函数.py """ python利用or在列表解析中调用多个函数.py 2016年3月15日 05:08:42 codegay & ...
- 利用echo命令实现倒计时的功能
echo -e:启用反斜线控制字符的转换 -E:关闭反斜线控制字符的转换(预设如此) -n:取消行末之换行符号(与 -e 选项下的 \c 字符同意 -e参数下的控制参数 \ ...
- java翻译到mono C#实现系列(4) 利用CountDownTimer类实现倒计时功能 mono版
群里的朋友问利用CountDownTimer类实现倒计时功能怎么实现,我就百度了,参考http://blog.csdn.net/qq344429461/article/details/7521361写 ...
- 利用kvo对集合进行操作
利用kvo对集合进行操作 NSLog(@"其他学生的成绩%@", [array valueForKeyPath:@"point"]); NSLog(@" ...
- python3之利用字典和列表实现城市多级菜单
利用字典和列表实现城市多级菜单 #coding:utf-8 #利用字典和列表实现城市多级菜单 addrIndex = {":"福建"} addrDict = {" ...
- C利用可变参数列表统计一组数的平均值,利用函数形式参数栈原理实现指针运算
//描述:利用可变参数列表统计一组数的平均值 #include <stdarg.h> #include <stdio.h> float average(int num, ... ...
- 微信小程序 列表倒计时
最近要实现一个列表倒计时的功能,写了个demo 展示图 <view class="center colu"> <view class="time&quo ...
- 利用UICollectionView实现列表和宫格视图的切换
很多时候我们需要列表和宫格视图的来回切换,就像苹果的天气应用一样,我之前见过一个用tableview和collectionview来实现这种效果的,我本人不太喜欢这个,那么有没有更好的方法呢?答案是: ...
随机推荐
- hdu 5693 D Game
D Game HDU - 5693 众所周知,度度熊喜欢的字符只有两个:B 和D. 今天,它发明了一个游戏:D游戏. 度度熊的英文并不是很高明,所以这里的D,没什么高深的含义,只是代指等差数列[(等差 ...
- 洛谷P3070 [USACO13JAN]岛游记Island Travels
P3070 [USACO13JAN]岛游记Island Travels 题目描述 Farmer John has taken the cows to a vacation out on the oce ...
- [Xcode 实际操作]五、使用表格-(3)设置UITableView单元格图标
目录:[Swift]Xcode实际操作 本文将演示如何给表格行设置图标. 打开资源文件夹[Assets.xcassets], 在资源文件夹中导入两张图片:一张彩色,一张灰色,作为单元格的图标. [+] ...
- ios json转model的简单现实
在android开发中,可用第三方的转换库如gson等.当然在ios也有一些库如MJExtensiond等.在这里,我简单实现一下. 一.先建一个model并且继承NSObject,代码如下: cla ...
- jquery封装多棵并列树
起因:当前的树都是在一棵树上,应产品需求,现在需要将一级菜单并列开,然后往下铺,这样只好自己写了. demo图: 我直接封在了jquery上,此外还加了获取勾选数据的一些简单API. 思路:先把一级菜 ...
- JSP-用网页输出乘法表 三角形及菱形
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Technical support website
Technical support:Please leave a message if you have any questions or suggestions. email: swvrwafet@ ...
- idea dao使用@Mapper注解 业务类使用@Autowired 注入dao 爆红问题
实际项目跑起来无影响,但是看起来不太爽. 可以在dao类添加org.springframework.stereotype.Repository 注解 或者可以在service类中使用 javax.an ...
- 剑指Offer丑数问题
这是剑指第一次卡死我的题……记录一下 首先看题目: 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数 ...
- phpize使用方法
phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,下面介绍一个它的使用方法,需要的朋友可以参考下 安装(fastcgi模式)的时候,常常有这样一句命令: 代码如下: / ...