UIPickerView - 多行行 - 需要布局建议

时间:2013-12-11 05:07:30

标签: ios uipickerview

我想用2行制作选择器,我试着像a link,但我无法理解我需要做什么:当我向代码添加标签坐标时,在其上创建一个视图和2个标签,但是选择字段仍然像默认一样。如何更改选择字段大小?选择字段中的文本在其他行时具有更大的大小。抱歉我的英文。

enter image description here

- (UIView*)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UIView* v;
if (view)
    v = view;
else
{
    v = [[UIView alloc] init] ;

    UILabel* l1 = [[UILabel alloc] init];
    l1.tag = 11;
    [v addSubview: l1];

    UILabel* l2 = [[UILabel alloc] init];
    l2.tag = 12;
    l2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [v addSubview: l2];
}

UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 110, 35)];
l1.font = [UIFont systemFontOfSize:22]; // choose desired size
l1.text = [NSString stringWithFormat: @"row %d line 1", row];

l1.tag = 11;
[v addSubview: l1];

UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 34 , 110, 35)];
l2.font = [UIFont systemFontOfSize:14]; // choose desired size
l2.text = [NSString stringWithFormat: @"row %d line 2", row];

l2.tag = 12;
[v addSubview: l2];

return v;
}

更新

 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
  return yourViewHeight;
   }

 - (UIView*)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
  UIView* v;
 if (view)
    v = view;
  else
  {
    v = [[UIView alloc] initWithFrame:CGRectMake(0, 34, 110, 35)] ;

    UILabel* l1 = [[UILabel alloc] init];
    l1.tag = 11;
    [v addSubview: l1];

    UILabel* l2 = [[UILabel alloc] init];
    l2.tag = 12;
    l2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [v addSubview: l2];
}

UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 110, 35)];
l1.font = [UIFont systemFontOfSize:22]; // choose desired size
l1.text = [NSString stringWithFormat: @"row %d line 1", row];

l1.tag = 11;
[v addSubview: l1];

UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 34 , 110, 35)];
l2.font = [UIFont systemFontOfSize:14]; // choose desired size
l2.text = [NSString stringWithFormat: @"row %d line 2", row];

l2.tag = 12;
[v addSubview: l2];

return v;
}

http://i.picresize.com/images/2013/12/11/IiYqd.png

3 个答案:

答案 0 :(得分:6)

希望它会起作用......

  1. 实施此委托方法并返回视图的高度
  2.   
        
    • (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {          返回你的ViewHeight;   }
    •   
    1. 为您的标签和视图设置框架。标签的框架不超过视图的框架
    2. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
                //1. Create your view and set frame for the view 
                //2. Create your label 1 and label 2 set the frame for your labels
                //3. Add and return your view
        }
      

答案 1 :(得分:0)

您应该实施pickerView:rowHeightForComponent:选择器视图委托方法,并为自定义视图返回足够高的高度。

您还应在v中设置pickerView:viewForRow:forComponent:reusingView: method的高度。

最后,如果您重复使用视图,请不要继续添加更多标签。他们已经从重复使用的视角出现了。

答案 2 :(得分:0)

试试此代码

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
 return 50;
}

than after

- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
     UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 310, 50)];
lblTitle.numberOfLines=2;
lblTitle.textColor=[UIColor blackColor];
lblTitle.font=[UIFont systemFontOfSize:14];
lblTitle.text=[selectionList objectAtIndex:row];
}
return lblTitle;

}