日期选择列表
2020-05-20 本文已影响0人
我叫山鸡_
一个选择日期的表格,如图 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;
}
附上地址