简体   繁体   中英

custom image in UIPicker

I am trying to implement an image in a UIImagePicker that has only one component. I add an uimageview and a uilabel. The catch is that the first row should NOT have an image as well as the 3rd row which I believe I have done right. My problem is that the images for each row are different so to solve this I have put them into an array of images and then based on what row is being passed in, I display the image. However; it is not working for me and seems to display the same image over and over. Can anyone tell me why? Thanks in advance!

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;
}
   }
 }

EDITED CODE

 -(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;
 }
 }

Your problem is "theRow" - its not even defined in this method. You should just put a placeholder string in the array - that is at position 0 and 17 just put @"". Now you can use "row" to index into your array and all will be well.

EDIT: So your code implies you have 18 rows, want to show an image in most rows, but do something different for rows 0 and 17. Thus, you need an array of size 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]; 

The concept is to insure that you always have the same number of objects in the array as you have rows.

You can add an assert at the start of this method:

assert(row < [imageNames count]);

which will cause an exception if what you believe to be true turns out not to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM