ios – 将图像图标添加到UIPickerView行

我看了看网上的内容并没有找到太多!

我想知道你如何将图像放在UIPicker中,以便每个行都有不同的图像.

#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

解决方法

- (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,110,60)];
    [tmpView insertSubview:temp atIndex:0];
    [tmpView insertSubview:channelLabel atIndex:1];

    return tmpView;
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...