现在,选择器只显示被选择的数据。我希望它在所选值之后有一个词,就像 iPhone 时钟应用程序在选择器上有“小时”和“分钟”一样。
最佳答案
在这里,我得到了这个问题的正确答案(我已经为一个 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;
}
在 .h
文件中取一个 int
变量命名为 lastSelectedIndex
- (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;
}
我已经测试过了。希望它能帮助你.... :)
关于iphone - 如何让我的 UIPickerView 在选定值之后显示标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5807411/