简体   繁体   中英

uipickerview selection on tableviewcell detailtextlabel

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 pickerData array 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!

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.

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

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]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM