我想在UITableView方法-didSelectRowAtIndexPath上显示选取器视图。这是一个样本设计(我需要显示食品名称而不是日期和时间的自定义选择器)。NumberOfRowsInSection大于10。选择单元格时,将显示选取器视图并从选取器视图中选择值。我如何才能做到这一点?

发布于 2014-10-20 17:47:12
使用以下方法在TableViewController中实现UIPickerViewDataSource和UIPickerViewDelegate协议:
如果您希望以更个性化的方式表示数据,而不是使用"titleForRow“实现:
对于当用户选择选取器实现中的行时的事件处理:
Remmember to在创建时将UIPickerView的委托设置为“self”(这将在TableView的单元格中,因此您有许多选择来创建它,或者在故事板中创建包含UIPickerView的原型单元格,或者在“cellForRowAtIndexPath”中手动创建),并在TableViewController的头文件中遵守这些协议。
要实现从TableView中的常规TableViewCell到具有交互式UIPickerView的单元格的切换,您可能希望在TableViewController中使用"didSelectRowAtIndexPath“来刷新该单元格,您可以使用self.tableView reloadData或使用以下动画:
例如,您可以将名为'editingPickerView‘的BOOL变量初始设置为'NO’,在'didSelectRowForIndexPath‘方法中设置为:
if(indexPath.row == X) {
self.editingPickerView = !self.editingPickerView;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationFade];
}我使用故事板,所以我会有一个显示正常数据的原型单元格(没有UIPickerView),当被选中时会显示一个不同的原型单元格。同时,在'cellForRowAtIndexPath‘中,你需要有一种不同的方式来根据'editingPickerView’变量提供相同的行,有点像这样:
if(editingPickerView) {
//reuse the cell with the identifier 'UIPickerView_cell'
UIPickerView *picker = (UIPickerView*) [cell viewWithTag:69];
picker.delegate = self;
} else {
//reuse the normal cell with the identifier 'normal_cell' and show data
}如果当用户在选择器内的UIPickerView中选择一行时,您想更改为普通单元格when :didSelectRow:inComponent:您应该这样做:
self.editingPickerView = !self.editingPickerView;
//you should probably know 'a priori' what index path your picker would be in
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationFade];发布于 2014-10-20 18:10:18
将隐藏的文本字段添加到视图中。然后在viewDidload中:
self.dtmPicker = [[UIDatePicker alloc] init];
self.dtmPicker.datePickerMode = UIDatePickerModeDate;
[self.dtmPicker addTarget:self action:@selector(DateChanged:) forControlEvents:UIControlEventValueChanged];
self.lblDate.inputView = self.dtmPicker;我在用这个。这很简单:)
发布于 2014-10-20 18:25:16
这可能会有帮助。只需将UIDatePicker替换为UIPicker控件并添加适当的委托方法即可。网址:http://masteringios.com/blog/2013/10/31/ios-7-in-line-uidatepicker/3/
我想这就是你想要的。只需删除"UIColor+Custom.h“、"UIColor+Custom.m”及其引用即可避免错误。
https://stackoverflow.com/questions/26462224
复制相似问题