如何在UIpickerView的特定行中插入图像和文本? [英] How insert an images and text in specific rows of an UIpickerView?

查看:132
本文介绍了如何在UIpickerView的特定行中插入图像和文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了以下功能:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UIImage *img = [UIImage imageNamed:@"persona.jpeg"];
    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 = @"persona y";
    channelLabel.textAlignment = UITextAlignmentLeft;
    channelLabel.backgroundColor = [UIColor clearColor];

    UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
    [tmpView insertSubview:temp atIndex:0];
    [tmpView insertSubview:channelLabel atIndex:1];

    return tmpView; 
}

,结果如下:

我需要在特定行中插入图像和文本,知道如何操作吗?

非常感谢您的帮助。

I need to insert the image and text in a specific row, any idea how to do it?
Thank you very much for your help.

我的想法是填充图片数组和描述以填充UIPickerView

My idea is to fill an array of pictures and a description to fill the UIPickerView

推荐答案

请尝试将数据获取为数组 NSDictionary

Try to get your data as Array of NSDictionary.

字典结构: {图片:< imageName>,LabelText:< labelText>}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
    NSDictionary *dict =  [datasourceArray objectAtIndex:row];
    UIImage *img = [UIImage imageNamed:[dict valueForKey:@"Image"]]; // get image path from the dictionary
    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 = [dict valueForKey:@"LabelText"];
    channelLabel.textAlignment = UITextAlignmentLeft;
    channelLabel.backgroundColor = [UIColor clearColor];

    UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
    [tmpView insertSubview:temp atIndex:0];
    [tmpView insertSubview:channelLabel atIndex:1];

    return tmpView;
}

这篇关于如何在UIpickerView的特定行中插入图像和文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆