首页 > 解决方案 > 在 UITableViewCells 中显示倒计时的方式,在每个单元格中的倒计时应该是不同的时间

问题描述

    - (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 单元格上创建了​​一个倒计时计时器,但它只为计时器获取数组的最后一个元素,并为每个单元格递减相同的值,但我希望它为每个单元格获取数组的每个元素. 请帮忙。计时器功能只被触发一次。

标签: iosobjective-cuitableview

解决方案


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


推荐阅读