2012-04-12 56 views
0

我有一個UIPickerView與3個組件,我需要前2個組件顯示一個字符串,第三個顯示圖像。我能做到這一點通過實現兩個:UIPIckerView顯示圖像和字符串

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

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

或者我需要創建標籤和只實現viewForRow

回答

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; 


    }