问题
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