2017-04-18 54 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() 

我不再有这个问题。