日期选择列表

一个选择日期的表格,如图
image.png

左边是三个button,右边是个UITableView,自由选择时间

主要代码:
今天、明天、后天三个按钮 单选按钮

- (void)selectButtonAction:(UIButton *)sender
{
    for(int i=0;i<self.buttonArray.count;i++)
    {
        UIButton *btn = self.buttonArray[i];
        [btn setBackgroundColor:KGrayColor];
         btn.selected = NO;
    }
    sender.selected =YES;
    [sender setBackgroundColor:[UIColor whiteColor]];//选中button的颜色
    
    //  返回顶点
    [self.mainView setContentOffset:CGPointZero animated:YES];
    
   //  返回状态
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:self.selectCellID inSection:0];
    [self.mainView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    
    // 赋值
    if (sender.tag == 101)
    {
        self.currentDay = 0;
    }
    else if (sender.tag == 102)
    {
        self.currentDay = 1;
    }
    else if (sender.tag == 103)
    {
        self.currentDay = 2;
    }
}

获取某个时间的秒时间戳

- (NSString *)countDay:(NSInteger)day withHours:(NSInteger)hour
{
       NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
        NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
        [greCalendar setTimeZone: timeZone];

        NSDateComponents *dateComponents = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:[NSDate date]];
    
        //  定义一个NSDateComponents对象,设置一个时间点
        NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
        [dateComponentsForDate setDay:dateComponents.day+day];
        [dateComponentsForDate setMonth:dateComponents.month];
        [dateComponentsForDate setYear:dateComponents.year];
        [dateComponentsForDate setHour:hour];
        [dateComponentsForDate setMinute:00];

        NSDate *dateFromDateComponentsForDate = [greCalendar dateFromComponents:dateComponentsForDate];
        
        NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[dateFromDateComponentsForDate timeIntervalSince1970]];
    
    return timeSp;
}

附上地址

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。