我的问题是我的UITableViewCell
包含一个UILable
,而且我自定义的UITableView
有大约300到400条记录。我的要求是我需要运行倒计时器。数据来自服务同时需要更新本地数据。
任何解决方案都非常值得......
我的自定义标签代码:
#import <UIKit/UIKit.h>
@interface TimLabel : UILabel
@property (nonatomic, strong) NSTimer *startTimer;
@property (nonatomic) BOOL isDays;
-(void)configureTimeString:(NSString*)timeString;
- (void)startCounter;
@end
//
// TimLabel.m
// ap
//
// Created by xxxxx on 06/11/14.
// Copyright (c) 2014 xxx. All rights reserved.
//
#import "TimLabel.h"
@interface TimLabel ()
{
int secondsLeft;
}
@end
@implementation TimLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
// [UIView animateWithDuration:3.0f animations:
// ^{
// [self setTextColor:[UIColor redColor]];
// } completion:^(BOOL finished)
// {
//
// }];
}
*/
-(void)configureTimeString:(NSString*)timeString
{
NSArray * timerArray = [timeString componentsSeparatedByString:@":" ];
int hours, minutes, seconds;
hours = [[timerArray objectAtIndex:0 ] intValue];
minutes = [[timerArray objectAtIndex:1] intValue];
seconds = [[timerArray objectAtIndex:2] intValue];
secondsLeft =hours * 3600 + minutes * 60 + seconds;
// if (!self.isDays) {
[self startCounter];
// }
}
- (void)startCounter
{
self.startTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
if (![self.startTimer isValid]) {
[self.startTimer fire];
}
}
-(void) updateCountdown
{
int hours, minutes, seconds;
secondsLeft--;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
if (secondsLeft >= 0) {
self.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}
else
{
[self.startTimer invalidate];
}
[[NSRunLoop mainRunLoop] addTimer:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
@end
答案 0 :(得分:0)
朋友们终于找到了答案。
以下是答案:
<强> TimLable.h 强>
//
// TimLabel.h
// ap
//
// Created by xxxx on 06/11/14.
// Copyright (c) 2014 xxx. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface TimLabel : UILabel
@property (nonatomic, strong) NSTimer *startTimer;
- (void)configureCountDownTimer:(NSDate *)closeDate;
@end
<强> TimLable.m 强>
//
// TimLabel.m
// ap
//
// Created by xxxx on 06/11/14.
// Copyright (c) 2014 xxx. All rights reserved.
//
#import "TimLabel.h"
@interface TimLabel ()
{
int secondsLeft;
}
@property (nonatomic, strong) NSDate *closeDate;
@end
@implementation TimLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)configureCountDownTimer:(NSDate *)closeDate
{
[self.startTimer invalidate];
self.closeDate = closeDate;
NSDate *curDate = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:curDate toDate:closeDate options:0];
if (components.day <= 0)
{
[self setText:[NSString stringWithFormat:@"%02d:%02d:%02d", components.hour, components.minute, components.second]];
}
else if(components.day == 1)
{
[self setText:@"1day"];
}
else
{
[self setText:[NSString stringWithFormat:@"%ddays", components.day]];
}
if (!self.startTimer.isValid)
{
[self startCounter];
}
}
- (void)updateCountdown
{
NSDate *curDate = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:curDate toDate:self.closeDate options:0];
if (components.day <= 0)
{
if ((components.hour > 0)||(components.minute > 0)||(components.second > 0)) {
[self setText:[NSString stringWithFormat:@"%02d:%02d:%02d", components.hour, components.minute, components.second]];
}
else
{
[self setText:@"Live"]; // This is for my functionality purpose
[self.startTimer invalidate];
}
}
else if(components.day == 1)
{
[self setText:@"1day"];
}
else
{
[self setText:[NSString stringWithFormat:@"%ddays", components.day]];
// NSLog(@"%d days",components.day);
}
}
- (void)startCounter
{
self.startTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
if (![self.startTimer isValid]) {
[self.startTimer fire];
}
}
@end