Search code examples
objective-ciosuiviewuipickerview

UIPickerView custom view don't visualizate


I have custom view:

#import <UIKit/UIKit.h>
@interface materialView : UIView {
    IBOutlet UIImageView * _img;
    IBOutlet UILabel * _title;
}
- (id)initWithTitle:(NSString*)pTitle image:(UIImage *)pImage;
@end

and

#import "materialView.h"
@implementation materialView
- (id)initWithTitle:(NSString*)pTitle image:(UIImage *)pImage {
    CGRect frame = CGRectMake(0, 0, 295, 38);
    self = [super initWithFrame:frame];
    if (self) {
        [_img setImage:pImage];
        [_title setText:pTitle];
    }
return self;
}
@end

then I add this View to my UIPickerView:

- (id)init {
    self=[super init];
    if (self) {
        materialView * pl = [[materialView alloc] initWithTitle:@"Plastic" image:[UIImage imageNamed:@"aaa.jpeg"]];
        materialView * gl = [[materialView alloc] initWithTitle:@"Glass" image:[UIImage     imageNamed:@"bbb.jpeg"]];
        _loadedItems = [[NSArray arrayWithObjects:pl, gl, nil] retain];
    } 
return self;
}
...

pickerView.dataSource = self;
pickerView.delegate = self;
...

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

Problem is, that I have quite strange behavior. My View don't show in Picker View. Can any body help me?


Solution

  • First, in your:

    - (UIView *)pickerView:(UIPickerView *)pickerView
                viewForRow:(NSInteger)row
              forComponent:(NSInteger)component
               reusingView:(UIView *)view
    

    test that _loadedItews and the objects returned with objectAtIndex: are not nil? Your code looks reasonable otherwise without more context.

    Also set your custom view's userInteractionEnabled property to NO. It seems that if it is set to YES then custom views intercept the touches and the picker can't scroll to the tapped row.