UIPickerView 是一個選擇器控件驹针,可以生成單列的選擇器饥脑,也可生成多列的選擇器言疗,而且開發(fā)者完全可以自定義選擇項的外觀晴圾,因此用法非常靈活,可多行展示的滾動視圖噪奄,UIPickerView 繼承自 UIView死姚。
最近工作不是很忙,就跟一朋友提出需求(時分選擇勤篮,根據(jù)要求設(shè)置可選與不可選小時和分鐘)都毒,本來想著使用UIDatePicker,但是自定義顯示碰缔,使用UIPickerView比較方便账劲,UIPickerView的使用與UITableView比較相似。
1金抡,常用代理方法
//返回有幾列
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
??? return 40.0f;
}
//返回指定列的行數(shù)
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
??? if (component == 0)return self.hourArray.count;
??? return self.minuteArray.count;
}
//要修改picker滾動里每行文字的值及相關(guān)屬性瀑焦,分割線等在此方法里設(shè)置
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
? ? //設(shè)置分割線的顏色,這里設(shè)為隱藏
? ? for(UIView *singleLine in pickerView.subviews){
? ? ? ? if (singleLine.frame.size.height < 1){
? ? ? ? ? ? singleLine.backgroundColor = [UIColor clearColor];
? ? ? ? }
? ? }
? ? //設(shè)置文字的屬性
? ? UILabel *timeLabel = [UILabel new];
? ? timeLabel.textAlignment = NSTextAlignmentCenter;
? ? timeLabel.font = [UIFont systemFontOfSize:24.0];
? ? timeLabel.textColor =? [UIColor colorWithRed:56/255.0 green:56/255.0 blue:56/255.0 alpha:1.0];
? ? BOOL isOptional = NO;
? ? if (component == 0) {
? ? ? ? timeLabel.text = self.hourArray[row];
? ? ? ? if ([self.timeDic.allKeys containsObject:self.hourArray[row]]) {
? ? ? ? ? ? isOptional = YES;
? ? ? ? }
? ? }else{
? ? ? ? timeLabel.text = self.minuteArray[row];
? ? ? ? if ([self.timeDic.allKeys containsObject:self.selectHour]) {
? ? ? ? ? ? if ([[self.timeDic objectForKey:self.selectHour] containsObject:self.minuteArray[row]]) {
? ? ? ? ? ? ? ? isOptional = YES;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? if (!self.timeDic.allKeys.count)? isOptional = YES;
? ? if (!isOptional) {
? ? ? ? // 橫線的顏色跟隨label字體顏色改變
? ? ? ? NSMutableAttributedString *newTime = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",timeLabel.text]];
? ? ? ? [newTime addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newTime.length)];
? ? ? ? timeLabel.attributedText = newTime;
? ? }
? ? return timeLabel;
}
//返回指定列,行的高度梗肝,就是自定義行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
? ? return 40.0f;
}
//返回指定列榛瓮,行的寬度,就是自定義的寬度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
???
??? return CGRectGetWidth(pickerView.frame)/4;
}
這個要說一下巫击, 如果你設(shè)置寬度禀晓,UIPickerView的row 會根據(jù)列數(shù)自動平分UIPickerView的寬度精续,這樣當(dāng)你的列數(shù)少的時候,row 就是傾斜的匆绣, 要想row不傾斜驻右,就手動設(shè)置寬度, 這個寬度不會影響row的觸發(fā)區(qū)域大小崎淳。
//row發(fā)生改變都會觸發(fā)這個代理方法堪夭,用于獲取選擇row的值
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
???
??? if (component == 0) {
??????? self.selectHour = self.hourArray[row];
??????? [self.pickerView reloadComponent:component + 1];
??? }else{
??????? self.index = row;
??? }
???
??? [self getHint:self.index];
??? self.selectTime = [NSString stringWithFormat:@"%@:%@",self.selectHour,self.minuteArray[self.index]];
}
設(shè)置滾動到某列某個row 是否有動畫效果
? [self.pickerView selectRow:9 inComponent:0 animated:YES];
刷新方法
[self.pickerView reloadComponent:1];//刷新某列?
[self.pickerView reloadAllComponents];//刷新所有列