选择器视图在选择器视图中显示数字但未在标签上正确显示?
Posted
技术标签:
【中文标题】选择器视图在选择器视图中显示数字但未在标签上正确显示?【英文标题】:picker view shows numbers in pickerview but not showing properly on label? 【发布时间】:2009-10-11 08:41:14 【问题描述】:我已经从界面生成器中添加了一个pickerview。我正在使用这种方法来显示这样的数据......
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
if (pickerView == SpotPickerView) // don't show selection for the custom picker
// report the selection to the UI label
label.text = [NSString stringWithFormat:@"%d - %@ - %d",[pickerView selectedRowInComponent:0],
[RowArray objectAtIndex:[pickerView selectedRowInComponent:1]],
[pickerView selectedRowInComponent:2]];
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
NSString *returnStr = @"";
if (pickerView == SpotPickerView)
if (component == 0)
returnStr = [[NSNumber numberWithInt:row+1] stringValue];
else if(component==1)
returnStr = [RowArray objectAtIndex:row];
else
returnStr =[[NSNumber numberWithInt:row+1] stringValue];
return returnStr;
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
CGFloat componentWidth = 0.0;
if (component == 0)
componentWidth = 90.0;
else if (component == 1)
componentWidth = 100.0;
else
componentWidth = 90.0;
return componentWidth;
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
return 40.0;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
int count;
if (component == 0)
count = 15;
else if (component == 1)
count=[RowArray count];
else
count = 100;
return count;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
return 3;
这里是行数组
- (void)viewDidLoad
RowArray = [[NSArray arrayWithObjects:
@"A", @"B", @"C",
@"D", @"E", @"F", @"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",
@"U",@"V",@"W",@"X",@"Y",@"Z",
nil] retain];
但选择第一行会显示这一点。
我需要将值存储在数据库中,所以我需要 1 而不是零。
我如何重置我的选择器视图。意味着当我点击一个按钮时,它会自动到达每列中的第 0 个索引。
【问题讨论】:
【参考方案1】:label.text = [NSString stringWithFormat:@"%d - %@ - %d",[pickerView selectedRowInComponent:0],
[RowArray objectAtIndex:[pickerView selectedRowInComponent:1]],
[pickerView selectedRowInComponent:2]];
此代码正在从组件中的选定行中检索行号。数组是零索引的,pickerview(可能)使用数组来管理行。因此,第一行将为 0,第二行为 1,第三行为 2,依此类推。这很容易解决:
[pickerView selectedRowInComponent:0] + 1
您可以使用selectRow:inComponent:animated
以编程方式选择某些行如果您想在每列中选择第 0 个索引,只需执行以下操作:[pickerView selectRow:0 inComponent:0 animated:YES]
用于每个组件。
【讨论】:
以上是关于选择器视图在选择器视图中显示数字但未在标签上正确显示?的主要内容,如果未能解决你的问题,请参考以下文章