我创建了一个十进制数UIPickerView
。我希望第二个组件只是一个恒定的点串(。)成为一个分隔符,我的意思是:
1)它不移动
2)它的宽度小于其他组件
3)与其他组件相比,它的颜色不同
以下是代码:
#pragma mark - UIPickerView : Datasource Protocol
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if(component == 2)
return 10;
if(component == 1)
return 1;
else
return 1000;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if(component == 1)
return @".";
else
return [NSString stringWithFormat:@"%ld", (long)row];
}
我该怎么做?
答案 0 :(得分:1)
您可以使用UILable
并将文本设置为“。” (即Dot),在选择器视图中添加标签。
然后在相关代表中获取3个组件,并为第二个组件返回nil。
以下是代码:
在您的设计方法中,我是在viewDidLoad
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
label = [[UILabel alloc] initWithFrame:CGRectMake(145, 76, 36, 36)];
label.font = [UIFont boldSystemFontOfSize:40];
label.layer.cornerRadius = 18.0;
label.layer.masksToBounds = YES;
label.text = @".";
[label setTextColor:[UIColor darkGrayColor]];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[myPickerView addSubview:label];
然后在委托中就像这样
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component ==0) {
return self.arrItemsTop.count;
}else if (component == 1){
return 0;
}else
return self.arrItemsOther.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return self.arrItemsTop[row];
}
else if (component == 1){
return nil;
}
else{
return self.arrItemsOther[row];
}
}
注意:arrItemsTop
是保留左侧值的数组,arrItemsOthers
是保持右侧值的数组
OutPut:
希望有所帮助......
快乐的编码。