【发布时间】:2015-08-13 14:41:49
【问题描述】:
我有一个UICollectionView,它是UIScrollView 的subview(这是必需的,因为集合视图上方和下方的内容。
无论如何...
集合视图每行显示三个项目,开始时只有两行。
前五个项目是实际项目,最后一个项目是“查看更多”项目。
当最后一个项目被按下时,我会更改显示的项目数并更新集合视图...
NSMutableArray *indexPaths = [NSMutableArray array];
for (int i = 5 ; i<self.facilities.count ; i++) {
[indexPaths addObject:[NSIndexPath indexPathForItem:i inSection:0]];
}
// changing the tableCollapsed bool updates the number of items
if (self.isTableCollapsed) {
self.tableCollapsed = NO;
[self.collectionView insertItemsAtIndexPaths:indexPaths];
} else {
self.tableCollapsed = YES;
[self.collectionView deleteItemsAtIndexPaths:indexPaths];
}
然后我跑……
[self setHeightAnimated:YES];
这是干什么的……
- (void)setHeightAnimated:(BOOL)animated
{
NSInteger numberOfRows = (NSInteger) ceilf((CGFloat)[self getNumberOfItems] / self.numberOfItemsPerRow);
self.heightConstraint.constant = numberOfRows * self.rowHeight;
if (animated) {
[UIView animateWithDuration:0.2
animations:^{
[self layoutIfNeeded];
}];
} else {
[self layoutIfNeeded];
}
}
这总是有效并设置正确的高度,当它应该是YES 或NO 时,它会得到正确的animated 值。但是,它从不动画。它只是立即切换到新的高度。
我猜这是因为运行了一些布局代码或其他原因,但我想知道是否有人知道如何最好地做到这一点,以便高度随着我指定的动画正确变化。
谢谢
【问题讨论】:
标签: ios objective-c autolayout uicollectionview