Domanda

I'm trying to populate a UIPickerView with images. Here is where i create programmatically the PickerView and i fill a NSMutableArray with 35 images called 1.png, 2.png ..., 35.png

@interface ShowImagesPVC () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) NSMutableArray *arrayOfImages;
@end

- (void)viewDidLoad
{
    [super viewDidLoad];

    for(int i = 1; i < 36; i++){
        NSString *tmpString = [NSString stringWithFormat:@"%d.png",i];
        [_arrayOfImages addObject:[[UIImageView alloc]initWithImage:[UIImage imageNamed:tmpString]]];
    }

    UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-5, 0, 315, 200)];
    myPickerView.delegate = self;
    myPickerView.dataSource = self;
    myPickerView.showsSelectionIndicator = YES;
    [self.view addSubview:myPickerView];
}

now i've implemented these methods

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    return [_arrayOfImages objectAtIndex:row];
}

- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _arrayOfImages.count;
}

The problem is that the UIPicker is created but it's empty, so i tried to NSLog the method - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view and it seems it's never called.

Any idea why?

È stato utile?

Soluzione

try this

- (void)viewDidLoad
{
    [super viewDidLoad];
_arrayOfImages = [NSMutableArray array];
    for(int i = 1; i < 36; i++){
        NSString *tmpString = [NSString stringWithFormat:@"%d.png",i];
        [_arrayOfImages addObject:[[UIImageView alloc]initWithImage:[UIImage imageNamed:tmpString]]];
    }

    UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-5, 0, 315, 200)];
    myPickerView.delegate = self;
    myPickerView.dataSource = self;
    myPickerView.showsSelectionIndicator = YES;
    [self.view addSubview:myPickerView];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top