我有一个集合视图,出于某种目的,我想在动态更新单元格后重新调整高度。所以集合视图将显示所有单元格,不需要滚动。
我尝试了两种类型的代码,
首先是正常的:
[self.ProjectCollectionView reloadData];
[self.ProjectCollectionView layoutIfNeeded];
NSLog(@"setting contraint to %f from cache", weakself.ProjectCollectionView.contentSize.height);
#endif
self.ProjectCollectionViewHeightConstraint.constant = self.ProjectCollectionView.contentSize.height;
[self.ProjectCollectionView layoutIfNeeded];
但是contentSize为0:
来自缓存的setting contraint to 0.000000 from cache
我试过一个GCD:
[self.ProjectCollectionView reloadData];
__weak typeof(HomeViewController) *weakself = self;
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"setting contraint to %f from cache", weakself.ProjectCollectionView.contentSize.height);
weakself.ProjectCollectionViewHeightConstraint.constant = weakself.ProjectCollectionView.contentSize.height;
[weakself.ProjectCollectionView layoutIfNeeded];
});
这一次,它显示setting contraint to 410.000000 from cache
我很困惑,第一个代码也应该在主线程上运行,我添加一个断点并使用线程信息来显示:
(lldb) thread info
thread #1: tid = 0x439ee, 0x0000000106c1f411 MCompass`-[HomeViewController fetchUserModelCache](self=0x00007fd258447e50, _cmd=0x0000000106d1dd7b) + 465 at HomeViewController.m:245, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1
为什么会有所不同?
答案 0 :(得分:0)
重新加载数据时请使用以下功能
- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated; // transition from one layout to another