我有一个带有2个组件的UIPickerView。
我希望UILabel显示所选行。现在我有2个UILabels并排,但我认为有一个显示数据会更有意义。
UIPickerView的第一个组件是1-500的数组,第二个组件是字符串数组,只是“.0lb和1 / 2lb”。
pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( int i = 0 ; i <= 1000 ; ++i)
[pickerArray addObject:[NSString stringWithFormat:@"%d", i]];
pickerArrayHalf = [[NSMutableArray alloc]initWithCapacity:2];
[pickerArrayHalf addObject:@".0 lb"];
[pickerArrayHalf addObject:@"1/2 lb"];
我目前正在使用此代码输入2 UILabels:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (thePickerView.tag==1)
{
if (component == 0)
{
NSLog(@"Selected weight 1: %@. Index of array: %i", [pickerArray objectAtIndex:row], row);
NSString *weightSelected;
weightSelected = [NSString stringWithFormat:@"%@", [pickerArray objectAtIndex:row]];
weightLabel.text = weightSelected;
}
else
{
NSLog(@"Selected weight 2: %@. Index of array: %i", [pickerArrayHalf objectAtIndex:row], row);
NSString *weightSelected;
weightSelected = [NSString stringWithFormat:@"%@", [pickerArrayHalf objectAtIndex:row]];
weightLabel2.text = weightSelected;
}
}
目前1 UILabel超过2的想法是因为这个字符串应该是一个数字,如“50.0”或“200 1/2”等。
答案 0 :(得分:1)
使用此
选择第二列中的行时,可以附加字符串weightSelected = [weightSelected stringByAppendingFormat:@" %@", [pickerArrayHalf objectAtIndex:row]];
然后将其设置为单个UILabel。
答案 1 :(得分:1)
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (thePickerView.tag==1)
{
NSMutableString *labelText = [[NSMutableString alloc] initWithString:@""];
if (component == 0)
{
NSLog(@"Selected weight 1: %@. Index of array: %i", [pickerArray objectAtIndex:row], row);
[labelText appendFormat:@"%@", [pickerArray objectAtIndex:row]];
}
else
{
NSLog(@"Selected weight 2: %@. Index of array: %i", [pickerArrayHalf objectAtIndex:row], row);
[labelText appendFormat:@"%@", [pickerArrayHalf objectAtIndex:row]];
}
weightLabel2.text = labelText;
[labelText release];
}
已更新:
在.h文件中声明两个实例变量我正在命名并使用它们,如下所示
NSMutableString *firstComponentText;
NSMutableString *secondComponentText;
将viewDidLoad
中的可变字符串上的init分配为[[NSMutableString alloc]initWithString:@""];
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (thePickerView.tag==1)
{
if (component == 0)
{
NSLog(@"Selected weight 1: %@. Index of array: %i", [pickerArray objectAtIndex:row], row);
[firstComponentText setString:[pickerArray objectAtIndex:row]];
}
else
{
NSLog(@"Selected weight 2: %@. Index of array: %i", [pickerArrayHalf objectAtIndex:row], row);
[secondComponentText setString:[pickerArrayHalf objectAtIndex:row]];
}
weightLabel2.text = [firstComponentText stringByAppendingFormat:@" %@",secondComponentText];
}