Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add image icon to a UIPickerView row

I have looked and looked on the net and not found much on this!

I would like to know how you put images in a UIPicker so that each of the rows have a different image.

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController
    @synthesize pickerContent;

    @synthesize p1;


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    //////////

    - (NSMutableArray *)pickerContent
    {
        if(!pickerContent) {
            pickerContent = [[NSMutableArray alloc] initWithObjects:
                              [UIImage imageNamed:@"one.jpg"],
                              [UIImage imageNamed:@"two.jpg"],
                              [UIImage imageNamed:@"three.jpg"],
                              [UIImage imageNamed:@"four.jpg"],
                              [UIImage imageNamed:@"five.jpg"], nil];
        }
        return pickerContent;
    }


    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        if ([pickerView isEqual:p1])
        {
            return 4;
        }

            return 0;

    }
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {


        if ([pickerView isEqual:p1])
        {
            if (component == R1)return [self.pickerContent count];
            if (component == R2)return [self.pickerContent count];
            if (component == R3)return [self.pickerContent count];
            if (component == R4)return [self.pickerContent count];
        }

        return 0;
    }

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        if ([pickerView isEqual:p1])
        {
            if (component== R1)return [pickerContent objectAtIndex:row];
            if (component== R2)return [pickerContent objectAtIndex:row];
            if (component== R3)return [pickerContent objectAtIndex:row];
            if (component== R4)return [pickerContent objectAtIndex:row];
        }

     return 0;

    }
    @end
like image 512
Dave Avatar asked Mar 31 '13 07:03

Dave


1 Answers

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"your image number %@.png", (long)row]];
    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 = [NSString stringWithFormat:@"%@", [your array objectAtIndex:row]];
    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;
}
like image 57
Wilson Aguiar Avatar answered Oct 21 '22 03:10

Wilson Aguiar