2012-01-16 5 views
0

私はPoker-iPhone-Applicationで作業しています。すべてがうまく動作しますが、私は変更をしたいと思い:現時点では私は2つのコンポーネントでUIPickerViewを持っており、それが本当に良い作品:UIPickerViewはUIImageViewとUILabelの代わりに疑問符を表示します

関連する作業コードを

cardColor = [[NSArray alloc] initWithObjects:@"not known",@"Spades", @"Hearts", @"Diamonds", @"Clubs", nil]; // ColorArray 
cardValue = [[NSArray alloc] initWithObjects:@"not known",@"Ace",@"King", @"Queen",@"Jack",@"Ten",@"Nine",@"Eight",@"Seven",@"Six",@"Five",@"Four", @"Three",@"Two",nil]; //FaceValueArray 

#pragma mark - 
#pragma mark UIPickerView DataSource 



- (NSInteger) numberOfComponentsInPickerView: (UIPickerView *) pickerView { 
    return 2; 

} 

- (NSInteger) pickerView: (UIPickerView *)pickerView 
numberOfRowsInComponent: (NSInteger) component { 

    if(component == CARD_COLOR) 
     return [self.cardColor count]; 
    else 
     return [self.cardValue count]; 

} 

- (NSString *) pickerView: (UIPickerView *) pickerView 
       titleForRow: (NSInteger) row forComponent: (NSInteger) component { 
    if(component == CARD_COLOR) 
     return [self.cardColor objectAtIndex:row]; 
    else 
     return [self.cardValue objectAtIndex:row]; 
} 
#pragma mark - 
#pragma mark UIPickerView Delegate 


- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
    if(component == CARD_COLOR) return 130.00; 

    else return 190.00; 

} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: 
(NSInteger)row inComponent:(NSInteger)component { 
    NSLog(@"picked row: %i, component: %i", row, component); 

    if (component == CARD_COLOR) { 


     NSLog(@"the string: %@", [self.cardColor objectAtIndex:row]); 
     short color; 
     if ([self.cardColor objectAtIndex:row][email protected]"not known") { 
      color=INVALID; 
     } 
     if ([self.cardColor objectAtIndex:row][email protected]"Spades") { 
      color=SPADES_VALUE; 
     } 
     if ([self.cardColor objectAtIndex:row][email protected]"Hearts") { 
      color=HEARTS_VALUE; 
     } 
     if ([self.cardColor objectAtIndex:row][email protected]"Diamonds") { 
      color=DIAMONDS_VALUE; 
     } 
     if ([self.cardColor objectAtIndex:row][email protected]"Clubs") { 
      color=CLUBS_VALUE; 
     } 

     NSLog(@"value %i",color); 




    } 
    if (component == CARD_VALUE) { 

     NSLog(@"the string: %@", [self.cardValue objectAtIndex:row]); 
     short value; 


     if ([self.cardValue objectAtIndex:row][email protected]"not known") { 
      value=INVALID; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Ace") { 
      value=ACE_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"King") { 
      value=KING_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Queen") { 
      value=QUEEN_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Jack") { 
      value=JACK_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Ten") { 
      value=TEN_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Nine") { 
      value=NINE_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Eight") { 
      value=EIGHT_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Seven") { 
      value=SEVEN_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Six") { 
      value=SIX_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Five") { 
      value=FIVE_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Four") { 
      value=FOUR_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Three") { 
      value=THREE_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Two") { 
      value=TWO_VALUE; 
     } 

     NSLog(@"value %i",value); 

    } 

私は私のプロジェクトを実行すると上記のコードで、それは次のようになります。

working implementation without UIImageViews

実際の問題は、私はUIIで4までのインデックスに1をNSStringsを交換することを好むだろうとmageViews。何日か前、私は私がUILabelsによってすべてNSStringsを交換して使用

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

代わり

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 

のでこれを達成することができることを読みました。

は、だから私は、これまでに私のコードを変更:

私は、働いていないコードで自分のプロジェクトを実行すると、関連する-動作しないコードが

UILabel * colorPickerUnknownLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 145, 45)]; 
    [email protected]"not known"; 
    [colorPickerUnknownLbl setTextAlignment:UITextAlignmentLeft]; 
    colorPickerUnknownLbl.opaque=NO; 
    colorPickerUnknownLbl.backgroundColor=[UIColor clearColor]; 
    colorPickerUnknownLbl.textColor=[UIColor blackColor]; 
    colorPickerUnknownLbl.font = [UIFont boldSystemFontOfSize:20.0]; 
    UIImage * colorPicker1Img = [UIImage imageNamed: @"picker_color-01.png"]; 
    UIImage * colorPicker2Img = [UIImage imageNamed: @"picker_color-02.png"]; 
    UIImage * colorPicker3Img = [UIImage imageNamed: @"picker_color-03.png"]; 
    UIImage * colorPicker4Img = [UIImage imageNamed: @"picker_color-04.png"]; 

    self.cardColor = [[NSArray alloc] initWithObjects:colorPickerUnknownLbl ,colorPicker1Img,colorPicker2Img, colorPicker3Img, colorPicker4Img, nil]; // ColorArray 
    self.cardValue = [[NSArray alloc] initWithObjects:@"not known",@"Ace",@"King", @"Queen",@"Jack",@"Ten",@"Nine",@"Eight",@"Seven",@"Six",@"Five",@"Four", @"Three",@"Two",nil]; //FaceValueArray 

#pragma mark - 
#pragma mark UIPickerView DataSource 



- (NSInteger) numberOfComponentsInPickerView: (UIPickerView *) pickerView { 
    return 2; 

} 

- (NSInteger) pickerView: (UIPickerView *)pickerView 
numberOfRowsInComponent: (NSInteger) component { 

    if(component == CARD_COLOR) 
     return [self.cardColor count]; 
    else 
     return [self.cardValue count]; 

} 

- (UIView *) pickerView: (UIPickerView *) pickerView 
       viewForRow: (NSInteger) row forComponent: (NSInteger) component { 
    if(component == CARD_COLOR) 
     if(row==0)return [self.cardColor objectAtIndex:row]; 
     else { 
      UIImageView * imageView = [[UIImageView alloc] initWithImage:[self.cardColor objectAtIndex:row]]; 
      return imageView; 
     } 
    else { 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 145, 45)]; 

    //For right alignment of text,You can set the UITextAlignmentRight of the label. 
    //No need to set alignment to UITextAlignmentLeft because it is defaulted to picker data display behavior. 

    [label setTextAlignment:UITextAlignmentCenter]; 
    label.opaque=NO; 
    label.backgroundColor=[UIColor clearColor]; 
    label.textColor = [UIColor blackColor]; 
    UIFont *font = [UIFont boldSystemFontOfSize:20]; 
    label.font = font; 
    [label setText:[NSString stringWithFormat:@"%@", [self.cardValue objectAtIndex:row]]]; 
     return label; 
    } 
} 
#pragma mark - 
#pragma mark UIPickerView Delegate 


- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
    if(component == CARD_COLOR) return 130.00; 

    else return 190.00; 

} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: 
(NSInteger)row inComponent:(NSInteger)component { 
    NSLog(@"picked row: %i, component: %i", row, component); 

    if (component == CARD_COLOR) { 


     //NSLog(@"the string: %@", [self.cardColor objectAtIndex:row]); 
     short color; 
     if ([self.cardColor objectAtIndex:row]==[cardColor objectAtIndex:0]) { 
      color=INVALID; 
     } 
     if ([self.cardColor objectAtIndex:row]==[cardColor objectAtIndex:1]) { 
      color=SPADES_VALUE; 
     } 
     if ([self.cardColor objectAtIndex:row]==[cardColor objectAtIndex:2]) { 
      color=HEARTS_VALUE; 
     } 
     if ([self.cardColor objectAtIndex:row]==[cardColor objectAtIndex:3]) { 
      color=DIAMONDS_VALUE; 
     } 
     if ([self.cardColor objectAtIndex:row]==[cardColor objectAtIndex:4]) { 
      color=CLUBS_VALUE; 
     } 

     NSLog(@"value %i",color); 





    } 
    if (component == CARD_VALUE) { 

     NSLog(@"the string: %@", [self.cardValue objectAtIndex:row]); 
     short value; 


     if ([self.cardValue objectAtIndex:row][email protected]"not known") { 
      value=INVALID; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Ace") { 
      value=ACE_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"King") { 
      value=KING_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Queen") { 
      value=QUEEN_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Jack") { 
      value=JACK_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Ten") { 
      value=TEN_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Nine") { 
      value=NINE_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Eight") { 
      value=EIGHT_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Seven") { 
      value=SEVEN_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Six") { 
      value=SIX_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Five") { 
      value=FIVE_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Four") { 
      value=FOUR_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Three") { 
      value=THREE_VALUE; 

     } 
     if ([self.cardValue objectAtIndex:row][email protected]"Two") { 
      value=TWO_VALUE; 
     } 

     NSLog(@"value %i",value); 

    } 

それは次のようになります。

not-working implementation with (not appearing) UIImages

これは数時間で作業していますが、バグは見つかりません。あなたの誰かが私の問題のアイデアや別の解決策を持っていますか?

+0

は多分あなたの問題: '場合(行== 0)リターン[self.cardColor objectAtIndex:行] ; '?あなたは 'UIImage'を返しますが、それは' UIView'サブクラスではありません。 – jamapag

+0

私はこれらの行にUIViewを作成すると思います。 'if(コンポーネント== CARD_COLOR) if(row == 0)return [self.cardColor objectAtIndex:row]; else { UIImageView * imageView = [[UIImageView alloc] initWithImage:[self.cardColor objectAtIndex:row]]; 戻るimageView; } ' –

答えて

4

viewForRow代理人メソッドの名前が間違っています(reusingViewパラメータがありません)。これに代えて

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

それはこのようになります。この行の

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

ありがとうございます。それは今..働く:) –

関連する問題