//选择器的初始化
UIPickerView * pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 300)];
pickerView.dataSource = self; 数据源
pickerView.delegate = self;代理
//iOS7h后没有效果(是否显示指示器)
pickerView.showsSelectionIndicator = NO;
//返回选中的row数
NSInteger selectedRow = [pickerView selectedRowInComponent:0];
//刷新数据
[pickerView reloadAllComponents]
//选择行数(选择第0组第三行)
[pickerView selectRow:3 inComponent:0 animated:YES];
}
//一个pickerView有多少个组件(列)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
//一个组件有多少排
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.dataList.count;
}
//每排显示的题目
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return self.dataList[row];
}
//选中多少行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"您选中了第%@行",@(row));
}
3、UIPickerViewDelegate代理方法
//组件的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (component == 0) {
return 250;
}
return 50;
}
//默认显示当前时间
UIDatePicker * datePicker = [[UIDatePicker alloc] init];
//获取所有时区
NSLog(@"%@",[NSLocale availableLocaleIdentifiers]);
//设置时区
datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"];
//datePicker样式选择
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate * date = [formatter dateFromString:@"2015-02-03 09:21:22"];
datePicker.date = date;
//设置最小时间
datePicker.minimumDate = [date dateByAddingTimeInterval: - 24 * 60 * 60];
//设置最大时间
datePicker.maximumDate = [date dateByAddingTimeInterval: 24 * 60 * 60];
//设置时间间隔
datePicker.minuteInterval = 30;