1

As I wrote in the title, I am trying to write down the value selected on a pickerview (populated by an array) to the detailTextLabel of the cell I clicked to bring up the pickerview. (The pickerview is shown in an actionSheet).

What I tried to do was to set up two mutable arrays: pickerData and tableData. Initially, the pickerDataarray contains the values shown in the pickerview, and the tableData array has only one value.

When the following method is called, I'm trying to record the selection onto the tableData array, and then use this value to replace the original value on the detailTextLabel

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [tableData replaceObjectAtIndex:row withObject:[pickerData objectAtIndex:row]];
}

The detailTextLabel is populated by the tableData array, and this happens in the method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

using the line of code:

cell.detailTextLabel.text = [pickerData objectAtIndex:indexPath.row];

What am I doing wrong? any suggestions on how should I do this differently? I have been programming for only a month and I'm sure this has a simple solution. Thank You in advance!

Juan Andres
  • 433
  • 7
  • 19

3 Answers3

0

You said you are using tableData for updating detailTextLabel but when you have a look at your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath you are using pickerData cell.detailTextLabel.text = [pickerData objectAtIndex:indexPath.row]; Which is causing problem. You should use cell.detailTextLabel.text = [tableData objectAtIndex:indexPath.row]; and hope it will solve your problem.

fibnochi
  • 1,113
  • 1
  • 9
  • 26
  • oh I'm sorry, I did not realize I copied it like that, when I do use "tableData" the app crashes. So I left it as "pickerData" while I figure out what is wrong. – Juan Andres Feb 01 '13 at 14:52
0

You can access your detailTextLabel in your pickerView didSelectRow and then set your picker value to this label

UILabel *detailTextLabel=(UILabel*)[[tableView cellForRowAtIndexPath:indexPath] detailTextLabel ];

detailTextLabel.text=[pickerData objectAtIndex:row];
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
0

YOU NEED TO initialize the tableData NSMutableArray : self.tableData = [[[NSMutableArray alloc] init] autorelease];

 id)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
UITableViewCell *cell =[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0];
cell.detailTextLabel.text = [pickerData objectAtIndex:row];
    [self.tableData insertObject:[pickerData objectAtIndex:row] atIndex:index]
}