2015-01-21 38 views
0

我有一個集合視圖,出於某種目的,我想在動態更新單元格後重新調整高度。所以收集視圖將顯示所有單元格,不需要滾動。reloadData後設置集合視圖高度

我嘗試了兩種類型的代碼,

第一正常:

[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

請使用以下功能時,你永遠加載數據

- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated; // transition from one layout to another 
+0

你能否詳細說明嗎?簡單地給一個func名稱並沒有太大的幫助。 reloadData和這個func的順序是什麼? – Wingzero 2015-01-21 07:55:21

+0

首先在集合視圖中傳遞新的collectionViewLayout並重新載入數據,它會佔用您在setCollectionViewLayout中傳遞的新高度方法 – 2015-01-21 08:04:45

+0

您能否給出一個代碼演示並編輯您的答案?謝謝。這將幫助每個人看看你的答案,而不是閱讀評論 – Wingzero 2015-01-21 08:15:13

相關問題