使用DatePicker扩展和折叠UITableViewCells [英] Expanding and collapsing UITableViewCells with DatePicker
问题描述
我正在构建一个应用程序,让用户从UITableView中选择日期。 tableView是静态和分组的。我看过很多问题,其中包括这个,试图弄清楚如何完成这一点 - 但似乎没有任何效果最佳。苹果的日历应用程序具有非常流畅和漂亮的动画,我没有通过这些示例设法重新创建。
I'm building an app that lets the user select dates from a UITableView. The tableView is static and grouped. I've looked through many questions, including this one, trying to figure out how to accomplish this - but nothing seems to work optimal. Apple's calendar app features a very smooth and nice animation that none of the examples I've been through have managed to recreate.
这是我想要的结果:
有人可以指点一个教程,或者说明如何用最简洁直观的方法来完成这样一个平滑的动画,就像我们在日历应用中看到的那样?
Could someone point me to a tutorial or explain how I can accomplish such a smooth animation with the most concise and straight forward way of doing it, like we see in the calendar app?
谢谢alot!
Erik
推荐答案
我假设你正在使用storyboard,例子是 UIPickerView
:
在包含要填充的文本框的单元格下创建一个tableviewcell,将检查器中的单元格行高度设置为216.0,并向该单元格添加一个UIPickerView。
I assume you're using storyboard, the example is with UIPickerView
:
Create a tableviewcell right under the cell that contains the textfield you want to fill and set the cells row height to 216.0 in the inspector and add a UIPickerView to that cell.
接下来通过Outlet将UIPickerView连接到你r viewcontroller并将以下属性添加到您的ViewController.h中:
Next connect the UIPickerView via Outlet to your viewcontroller and add the following property to your ViewController.h:
@property (weak, nonatomic) IBOutlet UIPickerView *statusPicker;
@property BOOL statusPickerVisible;
在您的ViewController.m中执行 viewWillAppear
In your ViewController.m do in viewWillAppear
self.statusPickerVisible = NO;
self.statusPicker.hidden = YES;
self.statusPicker.translatesAutoresizingMaskIntoConstraints = NO;
添加两种方法:
- (void)showStatusPickerCell {
self.statusPickerVisible = YES;
[self.tableView beginUpdates];
[self.tableView endUpdates];
self.statusPicker.hidden = NO;
self.statusPicker.alpha = 0.0f;
[UIView animateWithDuration:0.25 animations:^{
self.statusPicker.alpha = 1.0f;
}];
}
- (void)hideStatusPickerCell {
self.statusPickerVisible = NO;
[self.tableView beginUpdates];
[self.tableView endUpdates];
[UIView animateWithDuration:0.25
animations:^{
self.statusPicker.alpha = 0.0f;
}
completion:^(BOOL finished){
self.statusPicker.hidden = YES;
}];
}
在 heightForRowAtIndexPath
/ p>
In heightForRowAtIndexPath
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = self.tableView.rowHeight;
if (indexPath.row == 1){
height = self.statusPickerVisible ? 216.0f : 0.0f;
}
return height;
}
在 didSelectRowAtIndexPath
/ p>
In didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
if (self.statusPickerVisible){
[self hideStatusPickerCell];
} else {
[self showStatusPickerCell];
}
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
这篇关于使用DatePicker扩展和折叠UITableViewCells的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!