0

我正在尝试在只有一个组件的 UIImagePicker 中实现图像。我添加了一个 uimageview 和一个 uilabel。问题是第一行不应该像第三行一样有图像,我相信我做得对。我的问题是每一行的图像都是不同的,所以为了解决这个问题,我将它们放入一个图像数组中,然后根据传入的行,我显示图像。然而; 它对我不起作用,似乎一遍又一遍地显示相同的图像。谁能告诉我为什么?提前致谢!

imageNames= [[NSArray alloc] initWithObjects:@"9_stars.png", @"8_stars.png",@"7_stars.png",@"6_stars.png",@"5_stars.png",@"4_stars.png", nil]; 

/

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

NSString *imageName= [imageNames objectAtIndex:theRow];
NSLog (@"the image name is %@", imageName);
NSLog (@"the current count is %d", theRow);

if(component == 0)
{
    if (row == 0 || row == 17)
    {

        UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
        channelLabel.text = [currentLottoNames objectAtIndex:row];
        channelLabel.textAlignment = UITextAlignmentLeft;
        channelLabel.backgroundColor = [UIColor clearColor];
        channelLabel.font = [UIFont boldSystemFontOfSize:20];
        channelLabel.backgroundColor = [UIColor clearColor];
        channelLabel.shadowColor = [UIColor whiteColor];
        channelLabel.shadowOffset = CGSizeMake (0,1);

        UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];

        [tmpView insertSubview:channelLabel atIndex:0];
        return tmpView;

    }
    else {
        UIImage *img = [UIImage imageNamed:imageName];
        UIImageView *temp = [[UIImageView alloc] initWithImage:img];        
        temp.frame = CGRectMake(120, 15,70, 27);

    UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
    channelLabel.text = [currentLottoNames objectAtIndex:row];
    channelLabel.textAlignment = UITextAlignmentLeft;
    channelLabel.backgroundColor = [UIColor clearColor];
    channelLabel.font = [UIFont boldSystemFontOfSize:20];
    channelLabel.backgroundColor = [UIColor clearColor];
    channelLabel.shadowColor = [UIColor whiteColor];
    channelLabel.shadowOffset = CGSizeMake (0,1);

    UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
   [tmpView insertSubview:channelLabel atIndex:0];
    [tmpView insertSubview:temp atIndex: 1];
    return tmpView;
}
   }
 }

编辑代码

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

NSString *imageName= [imageNames objectAtIndex:row]; // This is where the issue is I believe. What do I use to go through the whole image array?
NSLog (@"the image name is %@", imageName);
NSLog (@"the current count is %d", row);

if(component == 0)
{

       UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
        channelLabel.text = [currentLottoNames objectAtIndex:row];
        channelLabel.textAlignment = UITextAlignmentLeft;
        channelLabel.backgroundColor = [UIColor clearColor];
        channelLabel.font = [UIFont boldSystemFontOfSize:20];
        channelLabel.backgroundColor = [UIColor clearColor];
        channelLabel.shadowColor = [UIColor whiteColor];
        channelLabel.shadowOffset = CGSizeMake (0,1);

        UIImage *img = [UIImage imageNamed:imageName];
        UIImageView *temp = [[UIImageView alloc] initWithImage:img];        
        temp.frame = CGRectMake(120, 15,70, 27);

    UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
    channelLabel.text = [currentLottoNames objectAtIndex:row];
    channelLabel.textAlignment = UITextAlignmentLeft;
    channelLabel.backgroundColor = [UIColor clearColor];
    channelLabel.font = [UIFont boldSystemFontOfSize:20];
    channelLabel.backgroundColor = [UIColor clearColor];
    channelLabel.shadowColor = [UIColor whiteColor];
    channelLabel.shadowOffset = CGSizeMake (0,1);

    UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];

    [tmpView insertSubview:channelLabel atIndex:0];
    [tmpView insertSubview:temp atIndex: 1];
    return tmpView;
 }
 }
4

1 回答 1

0

你的问题是“theRow”——它甚至没有在这个方法中定义。您应该在数组中放置一个占位符字符串 - 即在位置 0 和 17 只需放置 @""。现在你可以使用“row”来索引你的数组,一切都会好起来的。

编辑:所以你的代码意味着你有 18 行,想要在大多数行中显示一个图像,但是对第 0 行和第 17 行做一些不同的事情。因此,你需要一个大小为 18 的数组。

imageNames= [[NSArray alloc] initWithObjects:
@"", // row 0 unused
// 1 - 6
@"9_stars.png", @"8_stars.png",@"7_stars.png",@"6_stars.png",@"5_stars.png",@"4_stars.png", nil]; 
// 7 - 12
@"9_stars.png", @"8_stars.png",@"7_stars.png",@"6_stars.png",@"5_stars.png",@"4_stars.png", nil]; 
// 13 - 16
@"9_stars.png", @"8_stars.png",@"7_stars.png",@"6_stars.png",
// 17
@"" // not used
// keep adding
nil]; 

这个概念是为了确保数组中的对象数量始终与行数相同。

您可以在此方法的开头添加断言:

assert(row < [imageNames count]);

如果您认为是真的结果不是,这将导致异常。

于 2012-08-21T01:37:45.750 回答