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



我在 Xcode 中创建了包含 3 列的选取器,2. 和 3. 列填充的数据基于第一列中的数据。它工作正常,数据显示正确,我唯一的问题是如何写入当前显示 2 的选定行的值。和 3.列到某个字符串?

谢谢。

使用 - [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
}

实现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];
}

相关内容

最新更新