簡體   English   中英

UITableView的uitableviewcell中的計時器標簽覆蓋了我的計時器標簽,因此它運行speed,如何控制速度? ios

[英]Timer lable in the uitableviewcell in the UITableView overrides my timer lable so it runs speed , how to control the speed? ios

我的問題是我讓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

朋友們終於找到了答案。

答案是:-

蒂姆·萊伯

//
//  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

蒂姆·萊伯

//
//  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM