【发布时间】:2024-01-03 19:37:01
【问题描述】:
Stack Overflow 上有很多关于UITableViewCell 高度动画的类似问题,但对于新的 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;
注意carouselHeightConstraint。这是内容视图的子视图(唯一的子视图)的高度限制。
例如,高度设置为230.0f。在第一次加载时,它看起来像:
然后,在 See All 操作中我想展开单元格,我的代码:
- (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) {
}];
}
如您所见,我尝试使用这个好的旧解决方案:
Can you animate a height change on a UITableViewCell when selected?
结果:
我的单元格缩小到44.0f高度。
问题:
为什么会这样?我希望看到我的细胞在自动布局的魔力下顺利扩展。
注意:
我不想使用-tableView:heightForRowAtIndexPath:。现在是自动布局时代,对吧?
【问题讨论】:
-
您仍然需要使用
tableView:heightForRowAtIndexPath:。这不是自动布局工作。 -
@Astoria,看起来很像自动布局工作。
-
@orkenstein 我完全同意现在应该使用自动布局来完成。我正在尝试做基本相同的事情:在单元格的 contentView 上设置约束并告诉表格视图为高度设置动画。但是,到目前为止,我无法让它工作。您找到解决此问题的方法了吗?
-
@Alex,我最终使用了
tableView:heightForRowAtIndexPath:。它现在看起来是唯一的解决方案。悲伤但真实。不要忘记正确更新约束。 -
@orkenstein 在发布该评论后,我确实设法解决了我的问题,并且我使用了纯粹的自动布局方法:不使用
tableView:heightForRowAtIndexPath:。如果您仍然感兴趣,可以查看我的解决方案here(ExpandableTableViewCell 和示例项目)。不过,它仍在进行中(但它确实有效)。
标签: ios objective-c uitableview ios8 autolayout