在每个单元倒计时中显示UITableViewCells倒计时的方式应该是在不同的时间

时间:2018-05-16 12:18:37

标签: ios objective-c uitableview

    - (void)viewDidLoad {
        [super viewDidLoad];
        tableData = [[NSArray alloc]initWithObjects:@"2018-05-16 06:00:00 p",@"2018-05-16 09:00:00 p",@"2018-05-16 11:00:00 p",@"2018-05-17 02:00:00 a",@"2018-05-17 04:00:00 a",nil];

       currDate = [NSDate date];
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
        NSLog(@"the current date and time is %@",currDate);

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss a"];
        NSString *fxdDate = @"2018-05-16 07:00:00 a";
        NSDate *StartDate = [ dateFormat dateFromString:[ NSString stringWithFormat:@"%@", fxdDate]];
       for(int i=0;i<tableData.count;i++)
        {

            EndDate = [dateFormat dateFromString:tableData[i]];
            NSDateComponents *components, *components2;
            NSInteger hours, minutesfromStart, minuteFromEnd,secondsFromEnd;

            components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: StartDate options: 0];
            hours = [components hour];
            minutesfromStart = [ components minute];

            components2 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: EndDate options: 0];

            minuteFromEnd = [ components2 minute];
            secondsFromEnd = [ components2 second];

            NSDateComponents *components1;
            NSInteger hours1, minutes1, Seconds1;
            components1 = [[NSCalendar currentCalendar] components: 
 NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate: currDate toDate: EndDate options: 0];
            hours1 = [components1 hour];
            minutes1 = [ components1 minute];
            Seconds1 = [ components1 second];

            currMinute = minutes1;
            currHours = hours1;
            currSeconds =Seconds1;

            NSLog(@"Time left is %ld:%ld:%ld", (long)hours1, (long)minutes1,(long)Seconds1);

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

   -(void)timerFired
{
       if(( currHours>=0 || currMinute >=0 || currSeconds>=0) && currSeconds>=0)
        {
            if(currSeconds==0 && currMinute>0)
            {
                currMinute-=1;
                currSeconds=59;
            }
            else if(currMinute==0 && currHours>0)
            {
                currHours-=1;
                currMinute=59;
            }
            else if(currSeconds>=0)
            {
                currSeconds-=1;
            }
            if(currSeconds>-1)

           [tableview reloadData];

        }


        else
        {
            [timer invalidate];
        }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Reuse";

    SimpleTableViewCell *cell = (SimpleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {

       NSArray *nib =[[NSBundle mainBundle]loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];
        cell=[nib objectAtIndex:0];
    }

    if(indexPath.row == 0)
    {
        cell.dynamicTimerLabel.text =[NSString stringWithFormat:@"%02ld:%02ld:%02ld",currHours,currMinute, currSeconds];



    }

    if(indexPath.row == 1)
    {
        cell.dynamicTimerLabel.text =[NSString stringWithFormat:@"%02ld:%02ld:%02ld",currHours,currMinute, currSeconds];

    }

    if(indexPath.row == 2)
    {
        cell.dynamicTimerLabel.text =[NSString stringWithFormat:@" %02ld:%02ld:%02ld",currHours,currMinute, currSeconds];

    }
    if(indexPath.row == 3)
    {

        cell.dynamicTimerLabel.text =[NSString stringWithFormat:@" %02ld:%02ld:%02ld",currHours,currMinute, currSeconds];

    }
    if(indexPath.row == 4)
    {

        cell.dynamicTimerLabel.text =[NSString stringWithFormat:@" %02ld:%02ld:%02ld",currHours,currMinute, currSeconds];

    }


    cell.TimerLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}

在上面的代码中,它是在UITableView单元格上创建一个倒计时器,但是它只为定时器取出数组的最后一个元素,并为每个单元格递减相同但我希望它取出数组的每个元素对于每个细胞。请帮忙。计时器功能只被触发一次。

3 个答案:

答案 0 :(得分:1)

1)让你的计时器每秒触发一次,它已经完成了,除了需要调用的只是reloadData。 2)在cellForRowAtIndexPath中进行计算,并根据indexPath.row计算该行的目标时间与当前时间之间的时间间隔,然后以小时:分钟:秒显示该时间间隔。目前,您的计时器正在触发,计算一个间隔,并且所有单元格都使用该间隔作为其信息。

答案 1 :(得分:0)

您需要安排一个方法在1秒后重复触发。并将currentIndex对象放在全局级别,并在执行计时器方法时递增它。

@interface YourCellClass(){
    NSInteger currentTimerIndex;
    NSTimer *timer;
    NSArray *arrayOfTimes;
    BOOL isTimerCompleted;
}
@property (strong, nonatomic) NSString *dateString;
@end
@implementation YourCellClass
-(void)layoutSubviews {
    [super layoutSubviews];
    //This line will call startTimer function after every 1 second.
    if (!isTimerCompleted && timer == nil) {
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];
    }
}
-(void)startTimer {

    // dateString should be assign from you cellForRowAtIndexPath method like this
    //cell.dateString = [dateArray objectAtIndexPath:indexPath];


    //Suppose you have pass 16/5/2018 9:12:45 as dateString
    //So below i am checking how much is the difference between providedDate and currentDate with respect to (hours, minuts and seconds)
    // if all get zero it means both dates have become equal so i am disabling the timer.
    // In real scenario its like your order delivery has been set to 9:12:45 and its 8:59:45 now so you have
    // 0 hours 13 minuts and 0 seconds
    // In the very next second your timer have
    // 0 hours 12 minutes and 59 seconds and so on.

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss a"];
    NSString *fxdDate = @"2018-05-16 07:00:00 a";
    NSDate *date = [ dateFormat dateFromString:self.dateString];


    NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: NSDate.date toDate: date options: 0];
    if (components.hour == 0 && components.minute == 0 && components.second == 0) {
        [timer invalidate];
        isTimerCompleted = YES;
    }else {
        NSString *newTimeString = [NSString stringWithFormat:@"%ld : %ld : %ld",components.hour,components.minute,components.second];
        //
    }
}

答案 2 :(得分:0)

 if(indexPath.row == 0)
        {
            currDate = [NSDate date];
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
            NSLog(@"the current date and time is %@",currDate);

            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss a"];
            NSString *fxdDate = @"2018-05-16 07:00:00 a";
            NSDate *StartDate = [ dateFormat dateFromString:[ NSString stringWithFormat:@"%@", fxdDate]];

            NSString *dynDate = @"2018-05-17 08:00:00 p";

            EndDate = [dateFormat dateFromString:dynDate];
                NSDateComponents *components, *components2;
                NSInteger hours, minutesfromStart, minuteFromEnd,secondsFromEnd;

                components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: StartDate options: 0];
                hours = [components hour];
                minutesfromStart = [ components minute];

                components2 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: EndDate options: 0];

                minuteFromEnd = [ components2 minute];
                secondsFromEnd = [ components2 second];

                NSDateComponents *components1;
                NSInteger hours1, minutes1, Seconds1;

                components1 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate: currDate toDate: EndDate options: 0];
                hours1 = [components1 hour];
                minutes1 = [ components1 minute];
                Seconds1 = [ components1 second];

                currMinute = minutes1;
                currHours = hours1;
                currSeconds =Seconds1;

                NSLog(@"Time left is %ld:%ld:%ld", (long)hours1, (long)minutes1,(long)Seconds1);

              timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

                 cell.dynamicTimerLabel.text =[NSString stringWithFormat:@"%02ld:%02ld:%02ld",currHours,currMinute, currSeconds];


            }





        if(indexPath.row == 1)
        {
            currDate = [NSDate date];
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
            NSLog(@"the current date and time is %@",currDate);

            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss a"];
            NSString *fxdDate = @"2018-05-16 07:00:00 a";
            NSDate *StartDate = [ dateFormat dateFromString:[ NSString stringWithFormat:@"%@", fxdDate]];

            NSString *dynDate = @"2018-05-17 09:00:00 p";

            EndDate = [dateFormat dateFromString:dynDate];
            NSDateComponents *components, *components2;
            NSInteger hours, minutesfromStart, minuteFromEnd,secondsFromEnd;

            components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: StartDate options: 0];
            hours = [components hour];
            minutesfromStart = [ components minute];

            components2 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: EndDate options: 0];

            minuteFromEnd = [ components2 minute];
            secondsFromEnd = [ components2 second];

            NSDateComponents *components1;
            NSInteger hours1, minutes1, Seconds1;

            components1 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate: currDate toDate: EndDate options: 0];
            hours1 = [components1 hour];
            minutes1 = [ components1 minute];
            Seconds1 = [ components1 second];

            currMinute = minutes1;
            currHours = hours1;
            currSeconds =Seconds1;

            NSLog(@"Time left is %ld:%ld:%ld", (long)hours1, (long)minutes1,(long)Seconds1);

            timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

            cell.dynamicTimerLabel.text =[NSString stringWithFormat:@"%02ld:%02ld:%02ld",currHours,currMinute, currSeconds];

        }

        if(indexPath.row == 2)
        {
            currDate = [NSDate date];
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
            NSLog(@"the current date and time is %@",currDate);

            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss a"];
            NSString *fxdDate = @"2018-05-16 07:00:00 a";
            NSDate *StartDate = [ dateFormat dateFromString:[ NSString stringWithFormat:@"%@", fxdDate]];

            NSString *dynDate = @"2018-05-17 11:00:00 p";

            EndDate = [dateFormat dateFromString:dynDate];
            NSDateComponents *components, *components2;
            NSInteger hours, minutesfromStart, minuteFromEnd,secondsFromEnd;

            components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: StartDate options: 0];
            hours = [components hour];
            minutesfromStart = [ components minute];

            components2 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: EndDate options: 0];

            minuteFromEnd = [ components2 minute];
            secondsFromEnd = [ components2 second];

            NSDateComponents *components1;
            NSInteger hours1, minutes1, Seconds1;

            components1 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate: currDate toDate: EndDate options: 0];
            hours1 = [components1 hour];
            minutes1 = [ components1 minute];
            Seconds1 = [ components1 second];

            currMinute = minutes1;
            currHours = hours1;
            currSeconds =Seconds1;

            NSLog(@"Time left is %ld:%ld:%ld", (long)hours1, (long)minutes1,(long)Seconds1);



         timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];



             cell.dynamicTimerLabel.text =[NSString stringWithFormat:@"%02ld:%02ld:%02ld",currHours,currMinute, currSeconds];
        }

        if(indexPath.row == 3)
        {
            currDate = [NSDate date];
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
            NSLog(@"the current date and time is %@",currDate);

            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss a"];
            NSString *fxdDate = @"2018-05-16 07:00:00 a";
            NSDate *StartDate = [ dateFormat dateFromString:[ NSString stringWithFormat:@"%@", fxdDate]];

            NSString *dynDate = @"2018-05-18 02:00:00 a";

            EndDate = [dateFormat dateFromString:dynDate];
            NSDateComponents *components, *components2;
            NSInteger hours, minutesfromStart, minuteFromEnd,secondsFromEnd;

            components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: StartDate options: 0];
            hours = [components hour];
            minutesfromStart = [ components minute];

            components2 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: EndDate options: 0];

            minuteFromEnd = [ components2 minute];
            secondsFromEnd = [ components2 second];

            NSDateComponents *components1;
            NSInteger hours1, minutes1, Seconds1;

            components1 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate: currDate toDate: EndDate options: 0];
            hours1 = [components1 hour];
            minutes1 = [ components1 minute];
            Seconds1 = [ components1 second];

            currMinute = minutes1;
            currHours = hours1;
            currSeconds =Seconds1;

            NSLog(@"Time left is %ld:%ld:%ld", (long)hours1, (long)minutes1,(long)Seconds1);

            timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

             cell.dynamicTimerLabel.text =[NSString stringWithFormat:@"%02ld:%02ld:%02ld",currHours,currMinute, currSeconds];
        }

        if(indexPath.row == 4)
        {
            currDate = [NSDate date];
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
            NSLog(@"the current date and time is %@",currDate);

            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss a"];
            NSString *fxdDate = @"2018-05-16 07:00:00 a";
            NSDate *StartDate = [ dateFormat dateFromString:[ NSString stringWithFormat:@"%@", fxdDate]];

            NSString *dynDate = @"2018-05-18 4:00:00 a";

            EndDate = [dateFormat dateFromString:dynDate];
            NSDateComponents *components, *components2;
            NSInteger hours, minutesfromStart, minuteFromEnd,secondsFromEnd;

            components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: StartDate options: 0];
            hours = [components hour];
            minutesfromStart = [ components minute];

            components2 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: currDate toDate: EndDate options: 0];

            minuteFromEnd = [ components2 minute];
            secondsFromEnd = [ components2 second];

            NSDateComponents *components1;
            NSInteger hours1, minutes1, Seconds1;

            components1 = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate: currDate toDate: EndDate options: 0];
            hours1 = [components1 hour];
            minutes1 = [ components1 minute];
            Seconds1 = [ components1 second];

            currMinute = minutes1;
            currHours = hours1;
            currSeconds =Seconds1;

            NSLog(@"Time left is %ld:%ld:%ld", (long)hours1, (long)minutes1,(long)Seconds1);


            timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

             cell.dynamicTimerLabel.text =[NSString stringWithFormat:@"%02ld:%02ld:%02ld",currHours,currMinute, currSeconds];
        }



-(void)timerFired

{

if(( currHours>=0 || currMinute >=0 || currSeconds>=0) && currSeconds>=0)
    {
        if(currSeconds==0 && currMinute>0)
        {
            currMinute-=1;
            currSeconds=59;
        }
        else if(currMinute==0 && currHours>0)
        {
            currHours-=1;
            currMinute=59;
        }
        else if(currSeconds>=0)
        {
            currSeconds-=1;
        }
        if(currSeconds>-1)
            time=[NSString stringWithFormat:@"%02ld:%02ld:%02ld",currHours,currMinute, currSeconds];
            [tableview reloadData];

    }


    else
    {
        [timer invalidate];
    }

}

它对我有用