Reading value from selected row in picker

[亡魂溺海] 提交于 2019-12-11 06:27:23

问题


i have created picker in Xcode with 3 columns, 2. and 3. column are filled with data based on the data in first column. It works fine, and data is displaying correctly, my only problem is how can i write value that is currently displaying ing selected row for 2. and 3. column to some string?

Thank you.


回答1:


Use -[UIPickerView selectedRowInComponent:] to determine which row is selected, and then return the object at that index within your data array.

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

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


    // Handle the selection
}



回答2:


Implement didSelectRow delegate

- (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];

}


来源:https://stackoverflow.com/questions/21148280/reading-value-from-selected-row-in-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!