簡體   English   中英

UIPicker中的自定義圖像

[英]custom image in UIPicker

我試圖在只有一個組件的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;
 }
 }

您的問題是“ theRow”-在此方法中甚至沒有定義。 您應該只在數組中放置一個占位符字符串-分別在位置0和17處放置@“”。 現在,您可以使用“行”來索引數組,一切都會好起來。

編輯:因此您的代碼意味着您有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]);

如果您認為是正確的,那么這將導致異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM