ios7 - UITableViewCell,内置UIPickerView和UIDatePicker [英] ios7 - UITableViewCell with UIPickerView and UIDatePicker built inline
问题描述
帖子的数量是处理主题:如何制作内联UIPickerView 。因为我很懒,所以任何人都可以指向我的代码片段。说实话,我发现Apple DateCell样本很迂腐 - 必须有一个更优雅的方法。
A number of posts are dealing with the subject: How to make an inline UIPickerView. As I am lazy, can anyone point me to a code snippet. To be honest, I find the Apple DateCell sample pedantic - there has to be an a more elegant method.
dateCell应用程序是一个好的起点吗?还是有其他更好的链接。我很感激任何建议。
Is the dateCell app a good place to start? or are there other better links. I would appreciate any advice.
如果您阅读此内容并且不理解我的要求/目标,请参阅上面引用的两篇帖子或只需下载 Apple Sample (需要开发帐号)。
If you read this and do not understand my requirements / goal, please see the two posts referenced above or simply download the Apple Sample (dev. account required).
推荐答案
我使用另一种 - 也许更简单 - 解决这个问题的解决方案。
I use another - maybe simpler - solution to solve this.
我们有两个单元格的图像
Image that we have two cells
- 日期标签单元格
- 日期选择器单元格
大多数魔法都在表格视图委托的 tableView:heightForRowAtIndexPath:
方法中:
Most of the "magic" is within the table view delegate's tableView:heightForRowAtIndexPath:
method:
- (CGFloat)tableView:(UITableView:)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat heightForRow = tableView.rowHeight;
if ([self isDatePickerRowAtIndexPath:indexPath]) {
heightForRow = (self.isDatePickerShown) ? heightOfDatePicker : 0.0;
}
return heightForRow;
}
所以你只需通过返回高度<$来隐藏日期选择器C $ C> 0.0 。
So you simply "hide" the date picker by returning a height of 0.0
.
在 tableView:didSelectRowAtIndexPath:
方法中进行切换:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self isDateLabelRowAtIndexPath:indexPath])
{
self.datePickerIsShown = ! self.isDatePickerShown;
[tableView beginUpdates];
[tableView endUpdates];
}
}
调用空 beginUpdates
endUpdates
阻止表再次调用 tableView:heightForRowAtIndexPath:
(动画)并且很好淡入或淡出日期选择器单元格。
Calling the empty beginUpdates
endUpdates
block forces the table to call the tableView:heightForRowAtIndexPath:
again (animated) and nicely fades in or out the date picker cell.
当日期选择器单元格是某个部分中的最后一个时,您也可能想要在隐藏日期选择器时将日期标签单元格的 separatorInset
更新为 UIEdgeInsetsZero
显示时的默认值。
When the date picker cell is the last one in a section you might also want to update the date label cell's separatorInset
to UIEdgeInsetsZero
when the date picker is hidden and to the default value when it's shown.
编辑:
为了完整性: datePickerIsShown
是一个简单的布尔值:
For completeness: datePickerIsShown
is a simple boolean:
@property(nonatomic, getter = isDatePickerShown) BOOL datePickerIsShown;
方法 isDateLabelRowAtIndexPath:
和 isDatePickerRowAtIndexPath:
只是帮助方法,它将给定的 indexPath
与相应单元格的已知索引路径进行比较:
The methods isDateLabelRowAtIndexPath:
and isDatePickerRowAtIndexPath:
are just helper methods that compare a given indexPath
to the known index path of the appropriate cell:
- (BOOL)isDatePickerRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([self.datePickerIndexPath compare:indexPath] == NSOrderedSame);
}
编辑2 :
还缺少一个额外步骤:确保设置日期选择器单元格的 clipsToBounds
属性到是
,否则你会得到一些视图故障。
There's one additional step missing: Make sure that you set the date picker cell's clipsToBounds
property to YES
, otherwise you'll get some view glitches.
这篇关于ios7 - UITableViewCell,内置UIPickerView和UIDatePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!