简体   繁体   English

UITableView和UIPickerView交互问题

[英]UITableView and UIPickerView interaction issues

I have a UITableView consisting of three rows - each row is a data field in a settings screen. 我有一个由三行组成的UITableView-每行是设置屏幕中的一个数据字段。 When the user clicks a row, I have a UIPickerView appear for them to select a value. 当用户单击一行时,出现UIPickerView供他们选择一个值。 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. 我正在使用UITableViewCellStyleValue1左侧是数据字段的名称,而右侧( detailTextLabel )应该是从选择器中选择的值。

I created a targetCell ivar to hold the selected cell. 我创建了一个targetCell ivar来保存选定的单元格。 In my picker, I use the below code in the didSelectRow method: 在选择器中,我在didSelectRow方法中使用以下代码:

    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: 首先是detailTextLabel文本为白色(尽管API表示应为蓝色?

    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]; 所以我尝试添加一个[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: 如果您可以看一下这段代码,并让我知道在选择器上选择一个值时获得以下结果的最简单方法:

  • cell detailTextLabel.text immediately updates to the selected value in black color 单元格detailTextLabel.text立即更新为黑色的所选值
  • the blue highlight on the active cell is maintained 保持活动单元上的蓝色突出显示

Thanks! 谢谢!

- (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: 感谢chown的回答-我的最终工作代码是:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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