ork*_*ein 20 objective-c uitableview ios autolayout ios8
关于UITableViewCell高度动画的Stack Overflow上有很多类似的问题,但是对于新的iOS8自动布局驱动的表视图没有任何效果.我的问题:
定制单元格:
@property (weak, nonatomic) IBOutlet iCarousel *carouselView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UIButton *seeAllButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *carouselHeightConstraint;
Run Code Online (Sandbox Code Playgroud)
注意carouselHeightConstraint.这是内容视图的子视图(唯一的子视图)的高度约束.
高度设置230.0f为例如.首次加载时,它看起来像:

然后,在查看所有操作我要扩展单元格,我的代码:
- (IBAction)seeAllButtonAction:(id)sender {
BOOL vertical = !self.carouselCell.carouselView.vertical;
self.carouselCell.carouselView.type = vertical ? iCarouselTypeCylinder : iCarouselTypeLinear;
self.carouselCell.carouselView.vertical = vertical;
[UIView transitionWithView:self.carouselCell.carouselView
duration:0.2
options:0
animations:^{
self.carouselCell.carouselHeightConstraint.constant = vertical ? CGRectGetHeight(self.tableView.frame) : 230;
[self.carouselCell setNeedsUpdateConstraints];
[self.carouselCell updateConstraintsIfNeeded];
[self.tableView beginUpdates];
[self.tableView endUpdates];
completion:^(BOOL finished) {
}];
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我尝试使用这个很好的旧解决方案:
您可以选择在UITableViewCell上设置高度变化动画吗?
结果如下:

我的细胞缩小到44.0f高度.
为什么会这样?我希望看到我的细胞能够通过自动计划的魔力顺利扩展.
注意:
我不想使用-tableView:heightForRowAtIndexPath:.这是自动布局的时代吧?
Ale*_*lex 45
以下对我有用:
制备:
在viewDidLoad告诉表视图使用自施胶细胞:
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44; // Some average height of your cells
Run Code Online (Sandbox Code Playgroud)像往常一样添加约束,但将它们添加到单元格中contentView!
动画高度变化:
假设您更改了约束constant:
myConstraint.constant = newValue;
Run Code Online (Sandbox Code Playgroud)
...或者您添加/删除约束.
要为此更改设置动画,请执行以下操作:
告诉contentView动画更改:
[UIView animateWithDuration: 0.3 animations: ^{ [cell.contentView layoutIfNeeded] }]; // Or self.contentView if you're doing this from your own cell subclass
Run Code Online (Sandbox Code Playgroud)然后通过动画告诉表视图对高度变化作出反应
[tableView beginUpdates];
[tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)第一步中0.3的持续时间似乎是UITableView在调用begin/endUpdates时用于其动画的持续时间.
奖励 - 在没有动画的情况下更改高度,无需重新加载整个表:
如果你想做同样的事情,但没有动画,那么这样做:
[cell.contentView layoutIfNeeded];
[UIView setAnimationsEnabled: FALSE];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled: TRUE];
Run Code Online (Sandbox Code Playgroud)
摘要:
// Height changing code here:
// ...
if (animate) {
[UIView animateWithDuration: 0.3 animations: ^{ [cell.contentView layoutIfNeeded]; }];
[tableView beginUpdates];
[tableView endUpdates];
}
else {
[cell.contentView layoutIfNeeded];
[UIView setAnimationsEnabled: FALSE];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled: TRUE];
}
Run Code Online (Sandbox Code Playgroud)
您可以查看我在此处选择扩展的单元格的实现(纯Swift和纯自动布局 - 真正的未来之路).
| 归档时间: |
|
| 查看次数: |
12586 次 |
| 最近记录: |