Objective c 在iphone应用程序中的PickerView中显示不正确的值。
这是我显示pickerview的代码Objective c 在iphone应用程序中的PickerView中显示不正确的值。,objective-c,ios,xcode,Objective C,Ios,Xcode,这是我显示pickerview的代码 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *pickerLabel = (UILabel *)view; CGSize limitSize = CGSizeMake(300.0f, 50.0f
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *pickerLabel = (UILabel *)view;
CGSize limitSize = CGSizeMake(300.0f, 50.0f);
CGSize textSize;
CGRect labelRect;
switch ([pickerView tag]) {
case 1: //Quiz picker
{
//m_txtPurpose.text=@"Test";
QuizGenre *quiz = [m_arrQuizGenre objectAtIndex:row];
NSString *title = [quiz m_quizGenreName];
textSize = [title sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:limitSize lineBreakMode:UILineBreakModeWordWrap];
labelRect = CGRectMake(0, 0, textSize.width, textSize.height);
NSLog(@"length:%i title:%@",[title length],title);
NSLog(@"h:%f w:%f",textSize.height,textSize.width);
if (pickerLabel == nil)
{
pickerLabel = [[[UILabel alloc] initWithFrame:labelRect] autorelease];
[pickerLabel setFont:[UIFont systemFontOfSize:14]];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setLineBreakMode:UILineBreakModeWordWrap];
[pickerLabel setTextAlignment:UITextAlignmentCenter];
[pickerLabel setText:title];
[pickerLabel setNumberOfLines:3];
}
break;
case 2: //Purpose picker
{
//cost.text = @"ABC";
Purpose *prp = [m_arrPurpose objectAtIndex:row];
NSString *title = [prp m_purposeName];
textSize = [title sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:limitSize lineBreakMode:UILineBreakModeWordWrap];
labelRect = CGRectMake(0, 0, textSize.width, textSize.height);
NSLog(@"length:%i title:%@",[title length],title);
NSLog(@"h:%f w:%f",textSize.height,textSize.width);
if (pickerLabel == nil)
{
pickerLabel = [[[UILabel alloc] initWithFrame:labelRect] autorelease];
[pickerLabel setFont:[UIFont systemFontOfSize:14]];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setLineBreakMode:UILineBreakModeWordWrap];
[pickerLabel setTextAlignment:UITextAlignmentCenter];
[pickerLabel setText:title];
[pickerLabel setNumberOfLines:3];
}
break;
default:
break;
}
}
}
return pickerLabel;
}
我的选择器视图在第6行后显示错误的值。
我的测验选取器视图中有13个值。
类似测验类型:1>艺术2>地理3>常识到数字13>电视。
在第6个值之后,它从第一个记录开始。这是图片
相反,它应该显示这样的东西。电影、音乐、电视、政治等等。
谢谢你的帮助。
谢谢我找到了解决这个问题的正确方法。这段代码所做的是在六条记录之后刷新选择器,并且条件“if(pickerLabel==nil)”变为false,因此它重复相同的记录。有一次我把这几行代码复制到了其他部分,它对我来说运行良好
else{
[pickerLabel setFont:[UIFont systemFontOfSize:14]];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setLineBreakMode:UILineBreakModeWordWrap];
[pickerLabel setTextAlignment:UITextAlignmentCenter];
[pickerLabel setText:title];
[pickerLabel setNumberOfLines:3];
}
希望它能对像我这样面临同样问题的人有所帮助。你检查过每一行的标题不同吗?伙计,我检查过了,每一行的标题不同。嗯,你的牙套好像有问题。复制/粘贴代码时是否忘记了这一点?在没有看到其余部分的情况下,我还认为您可以重写大部分代码,只需在switch语句中保留*title创建。其余的都可以到外面去。