【发布时间】:2023-03-18 22:46:01
【问题描述】:
现在,选择器只显示正在选择的数据。我希望它在所选值之后有一个单词,就像 iPhone 时钟应用程序在选择器上有“小时”和“分钟”一样。
【问题讨论】:
标签: iphone ios objective-c cocoa-touch
现在,选择器只显示正在选择的数据。我希望它在所选值之后有一个单词,就像 iPhone 时钟应用程序在选择器上有“小时”和“分钟”一样。
【问题讨论】:
标签: iphone ios objective-c cocoa-touch
在这里,我得到了这个问题的正确答案(我已经为一个 component 做了它,根据您的要求更改它)看看:
您需要实现该方法:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
//set your View. Here is an example ..
UIView *pickerviewtemp=[[UIView alloc] initWithFrame:CGRectZero];
UILabel *lbl=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]autorelease];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setText:[array_from objectAtIndex:row]];
[lbl setFont:[UIFont boldSystemFontOfSize:16]];
[pickerviewtemp addSubview:lbl];
return pickerviewtemp;
}
在名为 lastSelectedIndex 的 .h 文件中获取 int 变量
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
UILabel *label = (UILabel*)[pickerView viewWithTag:999+(component*lastSelectedIndex)]; // you can set your own tag...
[label removeFromSuperview];
UIView *view = [pickerView viewForRow:row forComponent:component];
UILabel *lbl2=[[[UILabel alloc] initWithFrame:CGRectMake(110, 0, 50, 50)]autorelease];
lbl2.tag = 999+(component*row);
[lbl2 setBackgroundColor:[UIColor clearColor]];
[lbl2 setText:@yourtext"];
[lbl2 setFont:[UIFont boldSystemFontOfSize:16]];
[view addSubview:lbl2];
lastSelectedIndex = row;
}
我已经测试过了。希望对您有所帮助.... :)
【讨论】:
你必须使用下面的方法自己创建两个标签。这是pickerView的委托方法,当pickerview被加载或reloadcomponent命令时自动调用。
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)theView{
UIView *pickerviewtemp=[[UIView alloc] initWithFrame:CGRectZero];
UILabel *lbl=[[[UILabel alloc] initWithFrame:yourrequiredframe]autorelease];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setText:*set text according to yourself*];
[lbl setFont:[UIFont boldSystemFontOfSize:16]];
[pickerviewtemp addSubview:lbl];
UILabel *lb2=[[[UILabel alloc] initWithFrame:yourrequiredframe]autorelease];
[lbl2 setBackgroundColor:[UIColor clearColor]];
[lbl2 setText:*set text according to yourself*];
[lbl2 setFont:[UIFont boldSystemFontOfSize:16]];
[pickerviewtemp addSubview:lbl2];
return pickerviewtemp;
}
【讨论】:
你必须做一个定制的UIPickerView
如果你需要的只是添加一些标签,你可以简单地添加到UIPickerView 两个UILabel 上,因为UIPickerView 是一个UIView...快速但肮脏...
【讨论】: