Cell中的时分选择器
cell.h
#import
NS_ASSUME_NONNULL_BEGIN
@interface TimeCell :UITableViewCell
+ (instancetype)initWithTableView:(UITableView*)tableView withIndexPath:(NSIndexPath*)indexPath;
//选中PickerView的值
@property (nonatomic,copy) NSString *string;
@end
NS_ASSUME_NONNULL_END
cell.m
#import "TimeCell.h"
@interface TimeCell ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) UIView *timeView;
@property (nonatomic, assign) NSInteger selectedHour;
@property (nonatomic, assign) NSInteger selectedMinute;
@property (nonatomic, strong) NSCalendar *calendar;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) UIView *contentV;
@property (nonatomic, strong) UIView *bgView;
@end
@implementation TimeCell
+ (instancetype)initWithTableView:(UITableView*)tableView withIndexPath:(NSIndexPath*)indexPath
{
staticNSString*cellIdentifier =@"TimeCell";
TimeCell*cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell) {
cell = [[TimeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.backgroundColor = BackGroundColor;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
if(self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
NSCalendar *calendar0 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
comps = [calendar0 components:unitFlags fromDate:[NSDate date]];
[selfsetCurrentDate:[NSDatedate]];
[self timeView];
[self pickerView];
[_timeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView).offset(0);
make.left.mas_equalTo(self.contentView).offset(10);
make.right.mas_equalTo(self.contentView).offset(-10);
make.height.mas_equalTo(198);
}];
_timeView.layer.cornerRadius = 5;
_timeView.layer.masksToBounds = YES;
[_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.timeView);
}];
}
return self;
}
- (UIView*)timeView{
if(!_timeView) {
_timeView=[[UIView alloc]init];
[self.contentView addSubview:_timeView];
_timeView.backgroundColor = CellBackGroundColor;
}
return _timeView;
}
- (UIPickerView*)pickerView
{
if (!_pickerView) {
_pickerView = [[UIPickerView alloc]init];
[self.timeView addSubview:_pickerView];
_pickerView.delegate=self;
_pickerView.dataSource=self;
}
return _pickerView;
}
#pragma mark -- UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
return2;
}
//每列返回的数据
- (NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch(component) {
case0:
return24;
break;
case1:
return60;
break;
default:
break;
}
return0;
}
#pragma mark -- UIPickerViewDelegate
//默认时间
-(void)setCurrentDate:(NSDate*)currentDate
{
//获取当前时间
NSCalendar *calendar0 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
comps = [calendar0 components:unitFlags fromDate:currentDate];
NSInteger hour=[compshour];
NSInteger minute=[compsminute];
_selectedHour = hour;
_selectedMinute = minute;
[self.pickerView selectRow:hour inComponent:0 animated:NO];
[self.pickerView selectRow:minute inComponent:1 animated:NO];
[self pickerView:self.pickerView didSelectRow:hour inComponent:0];
[self pickerView:self.pickerView didSelectRow:minute inComponent:1];
[self.pickerView reloadAllComponents];
}
-(UIView*)pickerView:(UIPickerView*)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView*)view
{
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*component/6.0, 0,SCREEN_WIDTH/6.0, 30)];
label.font=[UIFont systemFontOfSize:15.0];
label.tag=component*100+row;
label.textAlignment=NSTextAlignmentCenter;
switch(component) {
case0:
{
label.textAlignment = NSTextAlignmentRight;
NSString*hourStr = [NSString stringWithFormat:@"%ld",(long)row];
NSString*hour = [NSString stringWithFormat:@"时"];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",hourStr,hour]];
if(hourStr.length==1){ [attributedStringaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorcyanColor]range:NSMakeRange(0,1)]; [attributedStringaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorwhiteColor]range:NSMakeRange(1,1)];
}elseif(hourStr.length==2){ [attributedStringaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorcyanColor]range:NSMakeRange(0,2)]; [attributedStringaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorwhiteColor]range:NSMakeRange(2,1)];
}
label.attributedText= attributedString;
}
break;
case1:
{
label.textAlignment=NSTextAlignmentRight;
NSString*minuteStr = [NSStringstringWithFormat:@"%ld",(long)row];
NSString*minute = [NSStringstringWithFormat:@"分"];
NSMutableAttributedString*attributedString = [[NSMutableAttributedStringalloc]initWithString:[NSStringstringWithFormat:@"%@%@",minuteStr,minute]];
if(minuteStr.length==1) { [attributedStringaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorcyanColor]range:NSMakeRange(0,1)];[attributedStringaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorwhiteColor]range:NSMakeRange(1,1)];
}elseif(minuteStr.length==2){
[attributedStringaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorcyanColor]range:NSMakeRange(0,2)]; [attributedStringaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorwhiteColor]range:NSMakeRange(2,1)];
}
label.attributedText= attributedString;
}
break;
default:
break;
}
returnlabel;
}
- (CGFloat)pickerView:(UIPickerView*)pickerView widthForComponent:(NSInteger)component {
return ([UIScreen mainScreen].bounds.size.width-40)/2;
}
- (CGFloat)pickerView:(UIPickerView*)pickerView rowHeightForComponent:(NSInteger)component{
return 30;
}
// 监听picker
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch(component)
{
case0:
{
_selectedHour=row;
}
break;
case1:
{
_selectedMinute=row;
}
break;
default:
break;
}
_string =[NSString stringWithFormat:@"%.2ld:%.2ld",_selectedHour,_selectedMinute];
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[supersetSelected:selectedanimated:animated];
// Configure the view for the selected state
}
@end