我有自定义视图:
#import <UIKit/UIKit.h>
@interface materialView : UIView {
IBOutlet UIImageView * _img;
IBOutlet UILabel * _title;
}
- (id)initWithTitle:(NSString*)pTitle image:(UIImage *)pImage;
@end
和
#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
然后我将此视图添加到我的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];
}
问题是,我的行为很奇怪。我的视图不会显示在Picker View中。
任何人都可以帮助我吗?
答案 0 :(得分:1)
首先,在你的:
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
测试_loadedItews
和objectAtIndex:
返回的对象不是nil
?如果没有更多上下文,您的代码看起来很合理。
还将自定义视图的userInteractionEnabled属性设置为NO。似乎如果将其设置为YES,则自定义视图会拦截触摸,而选择器无法滚动到抽头行。