从选取器中的选定行读取值

时间:2014-01-15 21:10:34

标签: ios objective-c uipickerview

我在Xcode中创建了带有3列,2和3列的选择器,根据第一列中的数据填充了数据。它运行正常,数据正确显示,我唯一的问题是如何编写当前显示2和3.列的选定行的值到某个字符串?

谢谢。

2 个答案:

答案 0 :(得分:1)

使用 - [UIPickerView selectedRowInComponent:]确定选择了哪一行,然后在数据数组中的该索引处返回该对象。

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

   NSInteger selectedRow = [pickerView selectedRowInComponent:0];
    NSString *selectedPickerRow=[yourArrayOfElements objectAtIndex:selectedRow];


    // Handle the selection
}

答案 1 :(得分:0)

实施didSelectRow委托

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


  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
  UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  NSString *cellText = [NSString stringWithString: cell.textLabel.text];


   NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:row+1 inSection:0];
   UITableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:indexPath1];
   NSString *cell1Text = [NSString stringWithString: cell1.textLabel.text];

   NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:row+2 inSection:indexPath.section];
   UITableViewCell *cell2 = [self.tableView cellForRowAtIndexPath:indexPath2];
   NSString *cell2Text = [NSString stringWithString: cell2.textLabel.text];

}