UIPickerView中显示的图像显示/消失

时间:2014-04-05 14:50:58

标签: ios objective-c uipickerview

我在UIPickerView中显示图像时遇到问题。图像随机出现和消失,从不显示在选取器的“中间”行中。这是代码

@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

这是刚加载的Picker图片 enter image description here

这是Picker的一张照片 enter image description here

在中间一行的第一张照片中应该看到图标,但没有任何内容。

图标大小是64x64,所以我试图让第一个40x40(但不会改变任何东西,因为它应该在第一张图片的中间行,但你可以看到它是空白的)

1 个答案:

答案 0 :(得分:0)

不要让_arrayOfImages成为图像视图数组,而是将其设为图像数组,然后在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;
}