我已经让UIPicker完美地工作了。唯一的问题是,我不仅需要每一行的文本,而且我还需要一个图像在文本的左侧和一些额外的潜文本下的主要文本。我不知道怎么做,所以我希望有人能给我指出正确的方向?我真的很感激。下面是它应该是什么样子:

发布于 2015-10-08 18:55:08
通过查看T先生链接到的名为"CountryPickerDemo“的项目(看看它!)我意识到让图片和文本出现在一行的核心方法是:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)行forComponent:(NSInteger)组件reusingView:(UIView *)视图
下面是我在我的代码中是如何工作的:
//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“中键入您自己的图像名称以查看其外观。放置不同的图像,可能使用一个数组,这是您要解决的问题。
https://stackoverflow.com/questions/33000209
复制相似问题