这是我显示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);
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;
}
我的Picker视图在第6行后显示错误的值。 我的测验选择器视图中有13个值。 像测验类型:1&gt;艺术2&gt;地理3&gt; <13的一般知识>电视。 在第6个值之后,它从第一个记录开始。这是图像
他们应该展示一些像这样的东西。电影,音乐,电视,政治等等。 我们将不胜感激。 感谢
答案 0 :(得分:0)
我找到了解决此问题的正确方案。这段代码正在做的是在六条记录之后刷新了选择器并且这个条件“if(pickerLabel == nil)”变得错误因此它重复了相同的记录。我曾经将这行代码复制到其他部分,以及它对我的工作正常。
else{
[pickerLabel setFont:[UIFont systemFontOfSize:14]];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setLineBreakMode:UILineBreakModeWordWrap];
[pickerLabel setTextAlignment:UITextAlignmentCenter];
[pickerLabel setText:title];
[pickerLabel setNumberOfLines:3];
}
对于像我这样面临同样问题的人来说,这可能会有所帮助。