带图像的自定义选择器视图

时间:2012-02-09 06:11:54

标签: iphone xcode4 uipickerview

我了解了如何使用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;

}

1 个答案:

答案 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。