ios - 自定义 UIPicker - 将图像、文本和子文本放在一行中?

标签 ios objective-c uipickerview

我的 UIPicker 运行良好。唯一的问题是我不仅需要每一行的文本,而且我还需要文本左侧的图像和正文下方的一些额外的子文本。我不知道该怎么做,所以我希望有人能指出我正确的方向?我真的很感激。它应该是这样的:

enter image description here

最佳答案

通过查看Mr.T链接的名为“CountryPickerDemo”的项目(看一下!),我意识到使图片和文本出现在一行中的核心方法是:

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)组件 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”中输入您自己的图像名称以查看其外观。放置不同的图像(可能使用数组)是您需要解决的问题。

关于ios - 自定义 UIPicker - 将图像、文本和子文本放在一行中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33000209/

相关文章:

iOS5 Storyboard UIPageViewController

objective-c - 为什么从 NSData 读取字节时会根据获取字节的顺序得到不同的值?

objective-c - 如何在 Objective-C 中创建静态方法?

ios - UIPickerView 文本比例不够

iphone - 按钮点击事件中的 iRate

javascript - Bootstrap Popover Inside Popover 不起作用

objective-c - Xcode 构建失败 "Undefined symbols for architecture x86_64"

iphone - UIPickerView 的问题

ios - 相互切换 2 个 UIPickerview 的组件

ios - 按钮 IBaction 连接显示未知符号,应用程序出现异常,无法识别发送到实例的选择