0

我有一个带有 3 个组件的 UIPickerView,我需要前 2 个组件来显示字符串,而第三个组件来显示图像。我可以通过实现两者来做到这一点:

- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(NSInteger)component reusingView:(UIView *)view

还是我需要创建标签并只实施viewForRow

4

1 回答 1

5
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
      forComponent:(NSInteger)component reusingView:(UIView *)view
    {
UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 60, 32)];
firstLabel.text = [array1 objectAtIndex:row];
firstLabel.textAlignment = UITextAlignmentLeft;
firstLabel.backgroundColor = [UIColor clearColor];

UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 0, 60, 32)];
secondLabel.text = [array2 objectAtIndex:row];
secondLabel.textAlignment = UITextAlignmentLeft;
secondLabel.backgroundColor = [UIColor clearColor];

UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[countries objectAtIndex:row]]];

UIImageView *icon = [[UIImageView alloc] initWithImage:img];        
temp.frame = CGRectMake(170, 0, 30, 30);




UIView *tmpView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 32)] autorelease];
[tmpView insertSubview:icon atIndex:0];
[tmpView insertSubview:firstLabel atIndex:0];
[tmpView insertSubview:secondLabel atIndex:0];
[tmpView setUserInteractionEnabled:NO];
[tmpView setTag:row];
[channelLabel release];
[temp release];
return tmpView;


    }
于 2012-04-13T11:43:36.960 回答