2017-04-18 52 views
1

我有一個奇怪的行爲UICollectionViewUICollectionViewCell越過粘頭

在圖1中顯示了用戶向上滾動集合時的正確行爲:項目位於標題視圖下。

enter image description here [圖1]

當我重新加載搜索後的項目,某些項目滾動期間越過頭部。

enter image description here

我不知道有關原因... 任何意見或建議? 謝謝。

編輯

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section 
{ 
    return CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetWidth([UIScreen mainScreen].bounds) * 0.612f); 
} 
+0

你在使用UIViewController還是UICollectionViewController? –

+0

你能告訴我你的UICollectionView的最高限制在哪裏?這是在UIView頂部佈局或搜索欄底部? –

+0

@AbilashBNair UIViewController添加了UICollectionView – Fry

回答

0

你可以嘗試設置sectionHeadersPinToVisibleBounds =真財產上的UICollectionViewFlowLayout。因爲每個UICollectionView都有自己的佈局。

Apple doc

一個布爾值指示是否頭銷的集合視圖邊界的滾動期間的頂部。

也許它會有所幫助。將此代碼添加到viewDidLoad。

(self.yourCollectionView.collectionViewLayout as! UICollectionViewFlowLayout).sectionHeadersPinToVisibleBounds = true 
+1

請爲您的代碼添加一些解釋,以便它更有用。 – miken32

+0

我已添加一些說明。 :) –

0

內的UIViewController爲UICollectionView我的工作代碼HeaderView一起:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 20; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor blueColor]; 
    return cell; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(100, 100); 
} 


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath]; 

    return view; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section 
{ 
    return CGSizeMake(self.view.frame.size.width, 100); 
} 

故事板:

enter image description here

輸出:

enter image description here

請檢查。希望這會以其他方式幫助你。

0

對我而言,有效的方法是致電reloadData(),而不是嘗試使用動畫重新加載(reloadSections())。


對我來說,我有一個顯示/隱藏按鈕可以切換在項目數量和0(擴大部分的內容)之間的部分的行數。

當我更換

collectionView?.reloadSections([section]) 

collectionView?.reloadData() 

我不再有這個問題。