我有一个双选择器和两个NSInteger变量,用于更新选择器的每个组件所在的行。在iPhone模拟器中(我还没有在实际的iPhone上测试过),如果我选择行(即点击下方或上方),数字就可以了,但是当我旋转时,我收到的行号通常与实际相差一些。这就是我所拥有的:
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
if (component == kSuitComponent) {
UIImage *image = self.suitImages[row];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self theSuitRow];
return imageView;
}
else {
NSString *text = self.cardNumber[row];
UILabel *label = [[UILabel alloc] init];
[self getCardRow];
label.text = text;
return label;
}
}
-(void) getCardRow {
cardRow = [_doublePicker selectedRowInComponent:kCardNumberComponent];
}
-(void) theSuitRow {
suitRow = [_doublePicker selectedRowInComponent:kSuitComponent];
}
答案 0 :(得分:0)
应该是:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (component == kSuitComponent) {
UIImage *image = self.suitImages[row];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
return imageView;
}
else {
NSString *text = self.cardNumber[row];
UILabel *label = [[UILabel alloc] init];
label.text = text;
return label;
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == kCardNumberComponent) {
cardRow = row;
} else {
suitRow = row;
}
}