UIPickerView - 在iOS5中,不会为所有行调用didSelectRow

时间:2013-07-30 19:31:37

标签: ios5

注意:我已正确设置委托,UIPicker在ios6中完美运行。 在ios5中,仅当我选择row0或row5时才会调用didSelectRow:的回调。我有12行。

有没有人知道可能出现什么问题?

NOte:UIPicker的委托和数据源指向具有以下代码的类。

// returns the number of 'columns' to display.
        - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

            if ([pickerView isKindOfClass:[MIDatePicker class]]) {

                switch (self.pickermode) {
                    case MIDatePickerModeDayMonth:
                        return 2;
                        break;
                    case MIDatePickerModeDayMonthYear:
                        return 3;
                        break;

                    default:

                        break;
                }
            }

            return 3;
        }

        // returns the # of rows in each component..
        - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

            if ([pickerView isKindOfClass:[MIDatePicker class]]) {

                //month , day , year
                switch (component) {
                    case 0:{
                        return [self.monthsArray count];
                    }
                        break;
                    case 1:
                    {
                        return [self.daysArray count];
                    }

                    case 2:{
                        return [self.yearArray count];
                    }

                    default:
                        break;
                }
            }

            return 3;
        }

        // returns width of column and height of row for each component.
        - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

            // month , day ,year
            switch (component) {
                case 0:{
                    return self.picker.frame.size.width/2;
                }

                case 1:
                    return self.picker.frame.size.width/6;

                case 2:{
                    return self.picker.frame.size.width/3;
                }

                default:
                    break;
            }
            return self.picker.frame.size.width;
        }
        - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

            return pickerView.frame.size.height/5;
        }


        - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
            // month , day ,year
            switch (component) {
                case 0:{
                    return [NSString stringWithFormat:@" %@ ",[self.monthsArray objectAtIndex:row]];
                }
                case 1:
                    return [self.daysArray objectAtIndex:row];

                case 2:{
                    return [self.yearArray objectAtIndex:row];
                }

                default:
                    break;
            }
            return @"";

        }


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

1 个答案:

答案 0 :(得分:2)

在ios5中:现在我通过将float转换为整数来获取didSelectRow的所有调用。

我的观察:默认选取器高度为216.0。如果我将行高改为pickerView.frame.size.height/3 or pickerView.frame.size.height/4 or even pickerView.frame.size.height/6;原因,则同一段代码可以工作:216.0完全可以被3,4,6整除,当除以5时,它有0.2分。这就是我在pickerView.frame.size.height / 5的情况下只为row1和row6调用didSelect的原因。

体面修复可能是:

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

    return (int) pickerView.frame.size.height/5;
}