2016-10-25 4 views
0

私は、ポケモンの名前と画像をすべて保持するUIPickerビューを作成しようとしています。問題は、同じ行にイメージとテキストを表示することができないということです。これを行う最善の方法は何でしょうか?UIPickerViewのNSArrayとTextのNSArray

pokemonArray = @[@"Aerodactyl", @"Alakazam", @"Arbok", @"Arcanine", @"Articuna", @"Beedrill", @"Blastoise", @"Butterfree", @"Chansey", @"Charizard", @"Clefable", @"Cloyster", @"Dewgong", @"Ditto", @"Dodrio", @"Dragonite", @"Dugtrio", @"Electabuzz", @"Electrode", @"Exeggcutor", @"Farfetch'd", @"Fearow", @"Flareon", @"Gengar", @"Golbat", @"Golduck", @"Golem", @"Gyarados", @"Haunter", @"Hitmonchan", @"Hypno", @"Jolteon", @"Jynx", @"Kabutops", @"Kangaskhan", @"Kingler", @"Lapras", @"Lickitung", @"Machamp", @"Magmar", @"Magneton", @"Marowak", @"Mewtwo", @"Moltres", @"Mr Mime", @"Muk", @"Nidoking", @"Nidoqueen", @"Ninetales", @"Omastar", @"Onix", @"Parasect", @"Persian", @"Pidgeot", @"Pinsir", @"Poliwrath", @"Porygon", @"Primeape", @"Raichu", @"Rapidash", @"Raticate", @"Rhydon", @"Sandslash", @"Scyther", @"Seadra", @"Seaking", @"Slowbro", @"Snorlax", @"Starmie", @"Tangela", @"Tauros", @"Tentacruel", @"Vaporeon", @"Venomoth", @"Venusaur", @"Victreebel", @"Vileplume", @"Weezing", @"Wigglytuff", @"Zapdos"]; 
imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"Aerodactyl"],[UIImage imageNamed:@"Alakazam"],[UIImage imageNamed:@"Arbok"],[UIImage imageNamed:@"Arcanine"],[UIImage imageNamed:@"Articuna"],[UIImage imageNamed:@"Beedrill"],[UIImage imageNamed:@"Blastoise"],[UIImage imageNamed:@"Butterfree"],[UIImage imageNamed:@"Chansey"],[UIImage imageNamed:@"Charizard"],[UIImage imageNamed:@"Clefable"],[UIImage imageNamed:@"Cloyster"],[UIImage imageNamed:@"Dewgong"],[UIImage imageNamed:@"Ditto"],[UIImage imageNamed:@"Dodrio"],[UIImage imageNamed:@"Dragonite"],[UIImage imageNamed:@"Dugtrio"],[UIImage imageNamed:@"Electabuzz"],[UIImage imageNamed:@"Electrode"],[UIImage imageNamed:@"Electrode"],[UIImage imageNamed:@"Exeggcutor"],[UIImage imageNamed:@"Farfetch'd"],[UIImage imageNamed:@"Fearow"],[UIImage imageNamed:@"Flareon"],[UIImage imageNamed:@"Gengar"],[UIImage imageNamed:@"Golbat"],[UIImage imageNamed:@"Golduck"],[UIImage imageNamed:@"Golem"],[UIImage imageNamed:@"Gyarados"],[UIImage imageNamed:@"Haunter"],[UIImage imageNamed:@"Hitmonchan"],[UIImage imageNamed:@"Hypno"],[UIImage imageNamed:@"Jolteon"],[UIImage imageNamed:@"Jynx"],[UIImage imageNamed:@"Kabutops"],[UIImage imageNamed:@"Kangaskhan"],[UIImage imageNamed:@"Kingler"],[UIImage imageNamed:@"Lapras"],[UIImage imageNamed:@"Lickitung"],[UIImage imageNamed:@"Machamp"],[UIImage imageNamed:@"Magmar"],[UIImage imageNamed:@"Magneton"],[UIImage imageNamed:@"Marowak"],[UIImage imageNamed:@"Mewtwo"],[UIImage imageNamed:@"Moltres"],[UIImage imageNamed:@"Mr Mime"],[UIImage imageNamed:@"Muk"],[UIImage imageNamed:@"Nidoking"],[UIImage imageNamed:@"Nidoqueen"],[UIImage imageNamed:@"Ninetales"],[UIImage imageNamed:@"Omastar"],[UIImage imageNamed:@"Onix"],[UIImage imageNamed:@"Parasect"],[UIImage imageNamed:@"Persian"],[UIImage imageNamed:@"Pidgeot"],[UIImage imageNamed:@"Pinsir"],[UIImage imageNamed:@"Poliwrath"],[UIImage imageNamed:@"Porygon"],[UIImage imageNamed:@"Primeape"],[UIImage imageNamed:@"Raichu"],[UIImage imageNamed:@"Rapidash"],[UIImage imageNamed:@"Raticate"],[UIImage imageNamed:@"Rhydon"],[UIImage imageNamed:@"Sandslash"],[UIImage imageNamed:@"Scyther"],[UIImage imageNamed:@"Seadra"],[UIImage imageNamed:@"Seaking"],[UIImage imageNamed:@"Slowbro"],[UIImage imageNamed:@"Snorlax"],[UIImage imageNamed:@"Starmie"],[UIImage imageNamed:@"Tangela"],[UIImage imageNamed:@"Tauros"],[UIImage imageNamed:@"Tentacruel"],[UIImage imageNamed:@"Vaporeon"],[UIImage imageNamed:@"Venomoth"],[UIImage imageNamed:@"Venusaur"],[UIImage imageNamed:@"Victreebel"],[UIImage imageNamed:@"Vileplume"],[UIImage imageNamed:@"Weezing"],[UIImage imageNamed:@"Wigglytuff"],[UIImage imageNamed:@"Zapdos"], nil]; 

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row 
      forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    pokemonImageView.frame = CGRectMake(10, 10, 25, 25); 
    [pokemonImageView setImage:[UIImage imageNamed:[imageArray objectAtIndex:row]]]; 

    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(55, 10, 25, 25)]; 
    nameLabel.text = [pokemonArray objectAtIndex:row]; 

    nameLabel.textAlignment = NSTextAlignmentRight; 
    nameLabel.backgroundColor = [UIColor clearColor]; 

    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 367, 300)]; 

    [newView insertSubview:pokemonImageView atIndex:0]; 
    [newView insertSubview:nameLabel atIndex:1]; 

    return newView; 
} 

答えて

0

UIViewオブジェクトを作成し、このメソッドを介してこのUIViewオブジェクトをUIPickerViewに渡します。 以下のコードを使用してください。

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    UIView* newView; 
    //Insert anything in this view object and that will be visible to the screen 
    return newView; 
} 
0

@Syed e Hussainiは、画像とテキストを保持して表示する必要があると言いました。 UIPickerViewのテキストと画像を得るには、以下のコードを使用してください。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ 
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"your image %@.png", (long)row]]; 
    UIImageView *temp = [[UIImageView alloc] initWithImage:img]; 
    temp.frame = CGRectMake(-70, 10, 60, 40); 

    UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, -5, 80, 60)]; 
    channelLabel.text = [NSString stringWithFormat:@"%@", [your array objectAtIndex:row]]; 
    channelLabel.textAlignment = UITextAlignmentLeft; 
    channelLabel.backgroundColor = [UIColor clearColor]; 

    UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)]; 
    [tmpView insertSubview:temp atIndex:0]; 
    [tmpView insertSubview:channelLabel atIndex:1]; 

    return tmpView; 
}