1

我制作了 UIPickerView 并添加到 viewcontroller'view 中。

我手动调整了pickerview的宽度。

当您查看图片时,组件的内容宽度和组件宽度之间不匹配,并且

pickerview 的宽度和组件宽度。

有没有像 UILabel 的 sizeToFit 这样的方法?

expected code is :
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
        switch(component) {
            [pickerView.label sizeToFit];
        }
    }


================= Following is mycode ====================================================
- (void)viewDidLoad {

    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

    [super viewDidLoad];

    UIPickerView *pickerView = [[[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)] autorelease];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;

    [self.view addSubview:pickerView];


    activities = [[NSArray alloc] initWithObjects:@"sleeping", @"eating", @"working", @"studying", @"walking", @"thinking", @"considering", @"testing", @"talking", nil];
    feelings = [[NSArray alloc] initWithObjects:@"awesome", @"sad", @"happy", nil];

}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    switch(component) {
        case 0: return 100;
        case 1: return 200;
    }
}

在此处输入图像描述

4

1 回答 1

1

试试我的解决方案..我从来没有设置过每个组件的宽度和高度......UIPickerview 自己维护......所以我不必担心这个......试试这个只是评论名为“widthForComponent”的整个方法并确保您已正确使用所有这些 Picker 方法。“numberOfComponentsInPickerView”、“numberOfRowsInComponent”和“titleForRow”然后再次运行项目...让我知道它是否会解决您的问题

好的,现在看到 uipickerview 需要以下方法......你想要一些代码,那么这里就是

- (void)pickerView:(UIPickerView *)pV didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // you're code for when user select any item from picker
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{ 
    if(component == 0)
        return [activities count];
    else if(component == 1)
        return [feelings count];
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == 0)
        return [activities objectAtIndex:row];
    else if(component == 1)
        return [feelings objectAtIndex:row];
    else
           return @"";
}
于 2012-09-25T05:41:22.863 回答