UITableView 和 UIPickerView 交互问题
我有一个由三行组成的 UITableView - 每行都是设置屏幕中的一个数据字段。当用户单击一行时,我会出现一个 UIPickerView
供他们选择一个值。我正在使用 UITableViewCellStyleValue1
- 左侧是数据字段的名称,右侧 (detailTextLabel
) 应该是从选择器中选择的值。
我创建了一个 targetCell ivar 来保存选定的单元格。在我的选择器中,我在 didSelectRow
方法中使用以下代码:
self.targetCell.detailTextLabel.text = @"TEST";
该代码有效,但是我有几个表示问题。第一个是detailTextLabel文本是白色的(尽管API说它应该是蓝色的??!?)我修复了这个问题:
self.targetCell.detailTextLabel.textColor = [UIColor blackColor];
但是,直到我单击该单元格或另一个单元格之前,该文本不会出现在单元格中。所以我尝试添加一个 [self.tableView reloadData];
这可以刷新单元格并显示标签文本,但也删除了我想要保留的蓝色突出显示。
如果您可以查看此代码并让我知道在选择器上选择值时获得以下结果的最简单方法:
- 单元格
detailTextLabel.text
立即更新为所选内容 黑色值 - 保持活动单元格上的蓝色突出显示
谢谢!
- (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];
}
编辑添加最终代码解决方案:
感谢 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];
}
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:
- cell
detailTextLabel.text
immediately updates to the selected
value in black color - 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:
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: