iOS 倒计时

iOS 倒计时
//
//  RootViewController.m
//  MyTimerDemo
//
//  Created by huluo on 1/21/14.
//  Copyright (c) 2014 bluemobi.cn. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController
{
    UITableView  *timerTableView;
    NSMutableArray  *timeArr;
    
    NSTimer  *myTimer;
    NSDate  *startDate;
    NSDate*  futureDate;
}

- (void)dealloc
{
    [timerTableView  release];
    [timeArr  release];
    [myTimer invalidate];
    [super dealloc];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor  brownColor];
    
    timeArr=[[NSMutableArray  alloc]initWithCapacity:0];
    for (int i=0; i<5; i++) {
        int random=arc4random()%10000+1;//1~10000
        NSTimeInterval deltTime = (NSTimeInterval)random;
        futureDate = [[NSDate date] dateByAddingTimeInterval:deltTime]; //未来时间
        [timeArr  addObject:futureDate];
    }

    timerTableView=[[UITableView  alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    timerTableView.delegate=self;
    timerTableView.dataSource=self;
    [self.view  addSubview:timerTableView];

     myTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshTime) userInfo:nil repeats:YES];
}



#pragma mark ----tableView----

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [timeArr count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    UITableViewCell* cell=(UITableViewCell  *)[tableView  dequeueReusableCellWithIdentifier:@"cell"];
    if(cell==nil)
    {
        cell=[[[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]autorelease];
        
      
    }
     NSDate  *toDate=[timeArr  objectAtIndex:indexPath.row];
     startDate = [NSDate date];
    long  leftSeconds=[toDate timeIntervalSinceDate:startDate];
    NSInteger seconds = leftSeconds % 60;
    NSInteger minutes = (leftSeconds / 60) % 60;
    NSInteger hours = leftSeconds / (60 * 60);
    if (hours==0) {
        cell.textLabel.text = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
    }else{
    cell.textLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    }
    
    return cell;
}


//刷新时间
-(void)refreshTime
{
    startDate  = [ [ NSDate alloc] init ];
    [timerTableView reloadData];
}
 
iOS 倒计时

iOS 倒计时

上一篇:微信公众号支付报文示例


下一篇:Java 比较两张图片的相似度