Alex G
Alex G

Reputation: 2309

Set custom UIPickerView

I want to be able to set my UIPickerView to have 1 column and 10 rows. Within this column there is a title (UILabel) and an image beside it (the images are stars, from 1-3 stars being a possibility). I have put the images into an array called "showImages". I have put the UILabels into an NSString array called "currentNames". The indexes of both array correspond with each other. For some reason my code is not working when I implement

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

and I believe this is because I am using "row" to index the arrays which is incorrect. Does anyone know how I could do this properly? Thanks in advance, the rest of my code is below.

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

NSString *imageName= [imageNames objectAtIndex:row];

if(component == 0)
{
NSString *imageName= [imageNames objectAtIndex:row];
        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];
      i++;
      return tmpView;
     }
   }

Upvotes: 2

Views: 993

Answers (1)

rdelmar
rdelmar

Reputation: 104082

I don't know how that could be wrong -- the row parameter being passed in should go from 0 to [imageNames count] - 1, so it should never be out of range. If you log row does it give you a number higher than [imageNames count] - 1? You should probably log [imageNames count] in the pickerView:numberOfRowsInComponent: method too, to make sure it's giving you what you think it is.

Upvotes: 2

Related Questions