Frage

I'm having problems while displaying images in a UIPickerView. The images appear and disappear randomly and never show in the "middle" row of the picker. This is the code

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

@implementation ShowImagesPVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    _arrayOfImages = [NSMutableArray array];
    for(int i = 1; i < 36; i++){
        NSString *tmpString = [NSString stringWithFormat:@"%d.png",i];
        UIImageView *myIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];

        [myIcon setImage:[UIImage imageNamed:tmpString]];
        [_arrayOfImages addObject:myIcon];
    }

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

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}


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

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 45;
}


@end

This is a pic of the Picker just loaded enter image description here

and this is a pic of the Picker a bit rolled enter image description here

There should be the icons seen in the first pic over the middle row, but there is nothing.

The icon size is 64x64, so i tried to make the first one 40x40 (but doesn't change anything because it should be in the middle row in the first pic, but as you can se it's blank)

War es hilfreich?

Lösung

Instead of making your _arrayOfImages an array of image views, make it an array of images, and then do this in viewForRow:forComponent:reusingView:,

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    iv.image = _arrayOfImages[row];
    return iv;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top