Я работаю над приложением Poker-iPhone. Все работает просто отлично, но я хотел бы внести изменения: на данный момент у меня есть UIPickerView с двумя компонентами, и он работает очень хорошо:
соответствующий рабочий код
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]==@"not known") {
color=INVALID;
}
if ([self.cardColor objectAtIndex:row]==@"Spades") {
color=SPADES_VALUE;
}
if ([self.cardColor objectAtIndex:row]==@"Hearts") {
color=HEARTS_VALUE;
}
if ([self.cardColor objectAtIndex:row]==@"Diamonds") {
color=DIAMONDS_VALUE;
}
if ([self.cardColor objectAtIndex:row]==@"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]==@"not known") {
value=INVALID;
}
if ([self.cardValue objectAtIndex:row]==@"Ace") {
value=ACE_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"King") {
value=KING_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Queen") {
value=QUEEN_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Jack") {
value=JACK_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Ten") {
value=TEN_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Nine") {
value=NINE_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Eight") {
value=EIGHT_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Seven") {
value=SEVEN_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Six") {
value=SIX_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Five") {
value=FIVE_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Four") {
value=FOUR_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Three") {
value=THREE_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Two") {
value=TWO_VALUE;
}
NSLog(@"value %i",value);
}
Когда я запускаю свой проект с кодом выше, он выглядит так:
Фактическая проблема заключается в том, что я предпочел бы заменить строки NSSt в индексах с 1 по 4 на UIImageViews. Несколько дней назад я прочитал, что могу добиться этого, заменив все строки NSSt на UILabels и используя
- (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)];
colorPickerUnknownLbl.text=@"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]==@"not known") {
value=INVALID;
}
if ([self.cardValue objectAtIndex:row]==@"Ace") {
value=ACE_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"King") {
value=KING_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Queen") {
value=QUEEN_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Jack") {
value=JACK_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Ten") {
value=TEN_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Nine") {
value=NINE_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Eight") {
value=EIGHT_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Seven") {
value=SEVEN_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Six") {
value=SIX_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Five") {
value=FIVE_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Four") {
value=FOUR_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Three") {
value=THREE_VALUE;
}
if ([self.cardValue objectAtIndex:row]==@"Two") {
value=TWO_VALUE;
}
NSLog(@"value %i",value);
}
Когда я запускаю свой проект с нерабочим кодом, он выглядит так:
Я работаю над этим часами, но не могу найти ошибок. У кого-нибудь из вас есть идея или другое решение моей проблемы?