2

我希望能够将我的设置UIPickerView为 1 列和 10 行。在此列中,有一个标题 ( UILabel) 和旁边的图像(图像是星星,可能有 1-3 颗星)。我已将图像放入一个名为“showImages”的数组中。我已将其UILabels放入一个NSString名为“currentNames”的数组中。两个数组的索引相互对应。由于某种原因,我的代码在我实现时不起作用

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

我相信这是因为我使用“行”来索引不正确的数组。有谁知道我怎么能正确地做到这一点?在此先感谢,我的其余代码如下。

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

1 回答 1

2

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.

于 2012-09-24T02:11:25.417 回答