UICollectionViewCell遍历粘性标题

时间:2017-04-18 12:17:06

标签: ios uicollectionview uicollectionviewcell

UICollectionView我的行为很奇怪。

在图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);
}

4 个答案:

答案 0 :(得分:0)

您可以尝试在 UICollectionViewFlowLayout 上设置 sectionHeadersPinToVisibleBounds = true 属性。因为每个 UICollectionView 都有自己的布局。

Apple doc

一个布尔值,指示在滚动期间是否将标题固定到集合视图的顶部。

也许它会有所帮助。将此代码添加到 viewDidLoad。

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

答案 1 :(得分:0)

我的UICollectionView的工作代码以及UIViewController中的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

请检查一下。希望这会以其他方式帮助你。

答案 2 :(得分:0)

对我有用的是调用reloadData()而不是尝试使用动画重新加载(reloadSections())。

在我的情况下,我有一个显示/隐藏按钮来切换项目计数和0之间的部分行数(扩展部分的内容)。

当我更换

collectionView?.reloadSections([section])

collectionView?.reloadData()

我不再有这个问题。

答案 3 :(得分:0)

在我的案例中尝试了许多可行的解决方案之后,

collectionView.clipsToBounds = true