如何在UIpickerView的特定行插入图片和文字?(How insert an images a

2019-09-22 02:48发布

我已实现了以下功能:

- (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; 
}

其结果如下:

我需要插入的图片和文字中的特定行,任何想法,该怎么办呢?
非常感谢您的帮助。

我的想法是,以填补画面的阵列和说明,以填补UIPickerView

Answer 1:

尝试让你的数据作为ArrayNSDictionary

字典结构: {Image:<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;
}


文章来源: How insert an images and text in specific rows of an UIpickerView?