我看了看网上的内容并没有找到太多!
我想知道你如何将图像放在UIPicker中,以便每个行都有不同的图像.
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize pickerContent; @synthesize p1; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view,typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // dispose of any resources that can be recreated. } ////////// - (NSMutableArray *)pickerContent { if(!pickerContent) { pickerContent = [[NSMutableArray alloc] initWithObjects: [UIImage imageNamed:@"one.jpg"],[UIImage imageNamed:@"two.jpg"],[UIImage imageNamed:@"three.jpg"],[UIImage imageNamed:@"four.jpg"],[UIImage imageNamed:@"five.jpg"],nil]; } return pickerContent; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { if ([pickerView isEqual:p1]) { return 4; } return 0; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if ([pickerView isEqual:p1]) { if (component == R1)return [self.pickerContent count]; if (component == R2)return [self.pickerContent count]; if (component == R3)return [self.pickerContent count]; if (component == R4)return [self.pickerContent count]; } return 0; } - (Nsstring *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if ([pickerView isEqual:p1]) { if (component== R1)return [pickerContent objectAtIndex:row]; if (component== R2)return [pickerContent objectAtIndex:row]; if (component== R3)return [pickerContent objectAtIndex:row]; if (component== R4)return [pickerContent objectAtIndex:row]; } return 0; } @end
解决方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UIImage *img = [UIImage imageNamed:[Nsstring stringWithFormat:@"your image number %@.png",(long)row]]; UIImageView *temp = [[UIImageView alloc] initWithImage:img]; temp.frame = CGRectMake(-70,10,60,40); UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,-5,80,60)]; channelLabel.text = [Nsstring stringWithFormat:@"%@",[your array objectAtIndex:row]]; channelLabel.textAlignment = UITextAlignmentLeft; channelLabel.backgroundColor = [UIColor clearColor]; UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0,110,60)]; [tmpView insertSubview:temp atIndex:0]; [tmpView insertSubview:channelLabel atIndex:1]; return tmpView; }