UIPickerView:有没有办法阻止用户滚过某一行?

时间:2013-12-20 03:09:57

标签: ios objective-c uipickerview

我正在寻找一种方法来阻止UIPickerView在遇到某一行时向下滚动。它仍然可以向上滚动,但不会向下滚动。点击第三行我想执行此操作。这是代码:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSLog(@"You selected this: %@", [dataArray objectAtIndex: row]);
    selected= row;
    NSLog(@"selected %i",selected);
    self.textField.text = [dataArray objectAtIndex: row];

    if (row = 3){
         // stop scrolling any lower.
    }
}

如果您有任何疑问,请与我们联系。我感谢任何帮助或提示。

2 个答案:

答案 0 :(得分:0)

试试这个:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSLog(@"You selected this: %@", [dataArray objectAtIndex: row]);
    selected= row;
    NSLog(@"selected %i",selected);
    self.textField.text = [dataArray objectAtIndex: row];

    if (row > 2){
     [pickerView selectRow:2 inComponent:component animated:YES];
    }
}

答案 1 :(得分:0)

创建一个属性以跟踪用户是否已选择第3行(索引为2的那个)让我们在本例中将其称为row3Selected。

if (row == 2){
         self.row3Selected = YES;
         [self.pickerView reloadAllComponents];
    }

将numberOfRowsInComponent:方法修改为如下所示:

- (NSInteger)numberOfRowsInComponent:(NSInteger)component {
    if (self.row3Selected) {
        return 3;
    }else{
        return myArray.count // myArray is the array used to populate the picker with data
    }
}

这将使您的选择器仅显示数据源中的前3个对象。