我正在尝试在只有一个组件的 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;
}
}