我了解了如何使用Picker视图,单打和双打。我想学习如何使用图像进行Picker视图。滚动具有不同的图像而不是数字或单词来选择一个。谢谢。我想是使用这个代码,但它没有运行我,我找不到错误。谢谢你的帮助。
@synthesize picker, Array, image;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [self.Array count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.Array objectAtIndex:row];
}
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view
{
NSString *img_src = [NSString stringWithFormat:@"6-12AM.png", row];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:img_src]];
return image;
}
答案 0 :(得分:2)
您需要实施
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
UIPickerView委托的方法,用于返回给定行的UIImageView。
参考页here.
例如(没有重用视图的非常愚蠢的实现)
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSString * img_src = [NSString stringWithFormat:@"image_%d.png", row];
UIImageView * retval = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:img_src]] autorelease];
return retval;
}
假设您的资源中有图像名为image_ [num] .png。