答案 0 :(得分:1)
通过查看Mr.T链接的名为“CountryPickerDemo”的项目(看看它!)我意识到制作图片的核心方法&文本出现在一行是:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
以下是我在代码中的工作原理:
//Custom view and rows are created inside this method. There doesn't seem to be an easier way.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
//IF view doesn't exist, then create it.
if (!view)
{
//View creation.
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 30)]; //(0, 0, 280, 30)]; //x & y width & height
//Rows label creation.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 110, 24)]; //(35, 3, 245, 24)];
label.textColor = [UIColor blackColor];
label.tag = 1;
[view addSubview:label];
//Rows image creation.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)]; //(3, 3, 24, 24)];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.tag = 2;
[view addSubview:imgView];
}
//INPUT data into rows. P.S. I have TWO components; not just one like in the picture above. If you only need one component like in the picture above, then ignore the IF ELSE statements and just use the code inside the IF statement only.
if (component == 0)
{
//Load actual text into 1st label sections.
((UILabel*)[view viewWithTag:1]).text = [NSString stringWithFormat:@"%@", _componentOne[row]];
//Load actual images into 1st image sections.
((UIImageView*)[view viewWithTag:2]).image = [UIImage imageNamed:@"imageName.png"];
}
else
{
//Load actual text into 2nd label sections.
((UILabel*)[view viewWithTag:1]).text = [NSString stringWithFormat:@"%@", _componentTwo[row]];
//Load actual images into 2nd image sections.
((UIImageView*)[view viewWithTag:2]).image = [UIImage imageNamed:@"imageName.png"];
}
//EXAMPLE CODE.
//((UILabel*)[view viewWithTag:1]).text = [[self class] countryNames][(NSUInteger)row];
//NSString *imagePath = [NSString stringWithFormat:@"CountryPicker.bundle/%@", [[self class] countryCodes][(NSUInteger) row]];
//((UIImageView*)[view viewWithTag:2]).image = [UIImage imageNamed:imagePath];
return view;
}
注意:在占位符@“imageName.png”中输入您自己的图片名称以查看其外观。通过使用数组来放置不同的图像是你的理由。