How insert an images and text in specific rows of an UIpickerView?

本秂侑毒 提交于 2019-12-08 07:44:34

问题


I have implemented the following function:

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

and the result is the following:

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.

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


回答1:


Try to get your data as Array of NSDictionary.

Dictionary structure : {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;
}


来源:https://stackoverflow.com/questions/12251315/how-insert-an-images-and-text-in-specific-rows-of-an-uipickerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!