我正在尝试使用图像填充UIPickerView。
这是我以编程方式创建PickerView的地方,我填充了NSMutableArray
,其中有35个图像,名为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];
}
现在我实现了这些方法
- (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;
}
问题是UIPicker已创建,但它是空的,所以我尝试NSLog
方法- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
,似乎从未调用过。
知道为什么吗?
答案 0 :(得分:0)
试试这个
- (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];
}