Reputation: 2300
I have a UITableView consisting of three rows - each row is a data field in a settings screen. When the user clicks a row, I have a UIPickerView
appear for them to select a value. I am using the UITableViewCellStyleValue1
- the left side is the name of the data field, and the right side (detailTextLabel
) should be the value selected from the picker.
I created a targetCell ivar to hold the selected cell. In my picker, I use the below code in the didSelectRow
method:
self.targetCell.detailTextLabel.text = @"TEST";
Which works, however I have several presentational issues. The first was that the detailTextLabel text was white (although API says it should be blue??!?) I fixed this with:
self.targetCell.detailTextLabel.textColor = [UIColor blackColor];
However, the text does not appear in the cell until I click the cell or another cell. So I tried adding a
[self.tableView reloadData];
Which does the trick of refreshing the cell and getting the label text to appear, but also removes the blue highlighting which I want to maintain.
If you could look over this code and let me know the easiest way to get the below results when selecting a value on the picker:
detailTextLabel.text
immediately updates to the selected
value in black colorThanks!
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
self.targetCell.detailTextLabel.textColor = [UIColor blackColor];
self.targetCell.detailTextLabel.text = @"TEST";
[self.tableView reloadData];
}
Edited to add final code soluton:
Thanks to chown for the answer - my final working code is:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
self.targetCell.detailTextLabel.textColor = [UIColor blackColor];
self.targetCell.detailTextLabel.text = @"TEST";
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
[self.tableView reloadData];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:path.row inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}
Upvotes: 1
Views: 1906
Reputation: 52728
Try this:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
self.targetCell.detailTextLabel.textColor = [UIColor blackColor];
self.targetCell.detailTextLabel.text = @"TEST";
[self.tableView reloadData];
NSIndexPath *ip = [tableView indexPathForSelectedRow];
// assuming self is the tableViews delegate. Optional
[self tableView:tableView willSelectRowAtIndexPath:ip];
[tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableScrollPositionTop];
// assuming self is the tableViews delegate. Optional
[self tableView:tableView didSelectRowAtIndexPath:ip];
}
Upvotes: 1