2

I want to do somenthing with the UIPickerView but I simple can't see how I can solve this problem...

This example is similar to what I want, and easily understandable. I have two simple pickers, with numbers from 0 to 10. Then I have a textfield where the user inserts a number.

What I want is that when the users inserts, for example, 7, and then selects a number in one of the pickers, like 5, the other pickers moves to 2, so that the sum is 7. The same with 0 and 7, 3 and 4.

Basically, how to make a picker move, according to the other picker value selected, based on the input inserted before.

Thank you very much :)

4

3 回答 3

0

从此帖子修改的代码:在数组中找到添加到给定总和的数字对

下面的代码循环查找加起来为number(输入到文本字段中的数字)的可能对,然后在选择器中选择数字等于该对中的一个数字的行。该代码假定您的数据源包含从 0 到某个总数的升序数字。

NSInteger number = [[textField text] integerValue];

int i = 0;
int j = number-1
NSMutableArray *possiblePairs = [NSMutableArray array];
while(i < j){
   if (i + j == number) {
      possiblePairs[] = @[(i), @(j)];
   }
   else if (a[i] + a[j] <  number) i += 1;
   else if (a[i] + a[j] >  number) j -= 1;
}

[picker1 selectRow:[dataSource indexOfObject:@(i)] inComponent:0];
[picker2 selectRow:[otherDataSource indexOfObject:@(j)] inComponent:0];
于 2013-05-27T12:35:40.367 回答
0

u 添加数组并重新加载组件,如下所示:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (component == 0) {
        club=[[NSString alloc] initWithFormat:@"%@" , [Nations objectAtIndex:row]];
      [secondcomponearray addObject club];
        [pickerView reloadComponent:1];

}
于 2013-05-27T11:45:59.597 回答
0

试试吧,假设你有两个UIPickerView喜欢pickerview1pickerview2然后你可以像下面这样设置逻辑..

注意 1.:-如果您使用不同的 2 ,请使用以下UIPickerView代码..

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (thePickerView == pickerview1) {
        if ([yourTextfield.text integerValue] > row) {
            int selectVal = [yourTextfield.text integerValue] - row ;
            [pickerview2 selectRow:selectVal inComponent:0 animated:YES];
        }
        else{
            int selectVal = row - [yourTextfield.text integerValue] ;
            [pickerview2 selectRow:selectVal inComponent:0 animated:YES];
        }
    }  
}

UITextField 如果您在7中输入值,然后选择值5,那么您会在这里看到什么,pickerview1然后它的row 数字是4.

在这里,当您从7中减去4时,在您设置或选择行号后得到的是3,其值为2pickerview2

注 2:-如果您使用 1UIPickerView和 2 组件,则使用以下代码..

   - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

        if (component == 0) {
            if ([yourTextfield.text integerValue] > row) {
                int selectVal = [yourTextfield.text integerValue] - row ;
                [thePickerView selectRow:selectVal inComponent:1 animated:YES];
            }
            else{
                int selectVal = row - [yourTextfield.text integerValue] ;
                [thePickerView selectRow:selectVal inComponent:1 animated:YES];
            }
        }  
    }

所以你得到了你想要的准确输出..

我希望你明白了...

享受编码.. :)

于 2013-05-27T11:43:24.377 回答