本文介绍了自定义 UIPicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发具有 3 个 uipicker 的应用程序.我想在每个 uipicker 中实现图像.但是,如下图所示,它只影响一个 uipicker,而不是全部.我想知道如何自定义图像中显示的所有 uipicker 元素

I have been working on the app, that has 3 uipickers. I want to implement images in each uipicker. However as seen in the image attached below, it just affects only one uipicker, not all of them.I would like to know how I could able to custom all uipicker elements shown in the image

 - (void)loadView
    {
        picker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(30, 200, 250, 250)];
        picker1.delegate = self;
        picker1.dataSource = self;
        picker1.showsSelectionIndicator = YES;
        picker1.tag=1;


        picker2 = [[UIPickerView alloc] initWithFrame:CGRectMake(390, 200, 250, 250)];
        picker2.delegate = self;
        picker2.dataSource = self;
        picker2.showsSelectionIndicator = YES;
        picker2.tag=2;



        picker3 = [[UIPickerView alloc] initWithFrame:CGRectMake(750, 200, 250, 250)];
        picker3.delegate = self;
        picker3.dataSource = self;
        picker3.showsSelectionIndicator = YES;
        picker3.tag=3;


        self.view = [[UIView alloc] initWithFrame:CGRectZero];
        self.view.backgroundColor=[UIColor whiteColor];

        [self.view addSubview:picker1];
        [self.view addSubview:picker2];
        [self.view addSubview:picker3];


        UIImage *seven = [UIImage imageNamed:@"seven.png"];
        UIImage *bar = [UIImage imageNamed:@"bar.png"];


        for(int i=1; i<=4; i++)
        {
            UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven];
            UIImageView *barView = [[UIImageView alloc] initWithImage:bar];


            NSArray *imageViewArray = [[NSArray alloc] initWithObjects:
                                       sevenView,barView,nil];

            NSString *fieldName = [[NSString alloc] initWithFormat:@"column%d",i];
            [self setValue:imageViewArray forKey:fieldName];
        }

    }


    -(UIView *)pickerView:(UIPickerView *)pickerView
               viewForRow:(NSInteger)row
             forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        //NSLog(@"%d",component);
        NSLog(@"tag: %d",pickerView.tag);

        if(pickerView.tag==1)
        {   NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d",component+1];
            NSArray *array1 = [self valueForKey:arrayName];
            return [array1 objectAtIndex:row];
        }
        else if(pickerView.tag==2)
        {   NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d",component+1];
            NSArray *array2 = [self valueForKey:arrayName];
            return [array2 objectAtIndex:row];
        }
        else
        {   NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d",component+1];
            NSArray *array3 = [self valueForKey:arrayName];
            return [array3 objectAtIndex:row];
        }

    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
        // Release anything that's not essential, such as cached data
    }


    - (void)dealloc {

    }

    #pragma mark UIPickerViewDelegate methods

    - (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [NSString stringWithFormat:@"%d",row];
    }

    #pragma mark UIPickerViewDataSource methods

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pv
    {
        return 4;
    }


    - (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
    {
        return 2;
    }

    @end

推荐答案

视图只能有一个父级.但是您正在尝试将每个视图用于 3 个父级(3 个选择器).无论选择器如何,您都尝试为每个选择器的组件 X 使用相同的图像视图集.那行不通.

Views can only have one parent. But you are trying to use each view for 3 parents (the 3 pickers). Regardless of the picker, you try to use the same set of image views for component X of each picker. That won't work.

您需要三组数组,而不仅仅是一组.换句话说,您无法在选择器之间共享视图.

You need three sets of arrays, not just the one set. In other words, you can't share views between the pickers.

这篇关于自定义 UIPicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 06:20