2014-04-02 80 views
1

我有两个选择器视图p1和p2,并且在p1数据在解析之后出现,并且p2的值取决于在p1中选择的值,因此p2的值将会发生变化每次我们在p1中选择不同的不同值时,p2中的值也在解析后出现。所以为此,我需要在p1中选择的值的名称和ID,并且我得到了名称,因为我将收到写在文本框中的名称,但问题是如何根据名称获取ID。根据另一个pickerview的值选择更改pickerview的值

+0

我可以像在数据库中那样使用“where”关键字。 arr = [[字典对象:@“id”] where]; – Ricky

回答

2

您的titleForRow对于p2应该(a)确定在p1中选择了哪一行; (二)用它来确定返回什么字符串为标题,用row参数中p2

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    if (pickerView == self.p1) { 
     // do whatever you're doing for p1 
    } else if (pickerView == self.p2) { 
     NSInteger p1Row = [self.p1 selectedRowInComponent:0]; 

     // now lookup identifier in `p1` associated with row `p1Row` 

     // now that you have the identifier for `p1`, now look up the text strings 
     // for `p2` on the basis of (a) that identifier; and (b) the `row` number 
     // passed to this method 

     return ...; // now return the title 
    } 

    return nil; 
} 

显然,当你改变你的p1选择,你可以再重装p2。下面做一个快速的淡入淡出过渡,使过渡不太刺耳:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    if (pickerView == self.p1) { 
     [UIView transitionWithView:self.p2 duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ 
      [self.p2 reloadAllComponents]; 
      [self.p2 selectRow:0 inComponent:0 animated:NO]; 
     } completion:nil]; 
    } 
} 

不幸的是,特定代码的细节会有所不同基于模型支持你的选择器的看法,但希望这说明了基本思路。

相关问题