滚动时大 UICollectionViewCell 消失 [英] Large UICollectionViewCell disappear on scrolling

查看:30
本文介绍了滚动时大 UICollectionViewCell 消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了与 here 相同的问题,即大 UICollectionViewCell(显然是 UICollectionView 高度的两倍多)在给定的滚动偏移处消失,然后在给定的滚动偏移后重新出现.

I'm encountering the same problem as here which is that large UICollectionViewCell (more than twice of UICollectionView height apparently) disappear at a given scrolling offset and then reappear after a given scrolling offset also.

我已经实现了 @JonathanCichon 解决方案,即继承 UICollectionView 并在 _visibleBounds 上执行自定义操作代码>(我知道这是一个私有 API 但没关系,我不需要在 Apple Store 上提交)

I've implemented @JonathanCichon solution which is to subclass UICollectionView and perform custom action on _visibleBounds (I know it's a private API but no matter, i don't need to submit it on Apple Store)

这里是我的自定义集合视图:

Here my custom collection view :

#import "CollectionView.h"

@interface UICollectionView ()

- (CGRect)_visibleBounds;

@end

@implementation CollectionView

- (CGRect)_visibleBounds
{
    CGRect rect = [super _visibleBounds];
    rect.size.height = [self heightOfLargestVisibleCell];
    return rect;
}

- (CGFloat)heightOfLargestVisibleCell
{
    // get current screen height depending on orientation
    CGFloat screenSize = [self currentScreenHeight];

    CGFloat largestCell = 0;

    NSArray *visibleCells = self.visibleCells;

    // get the largest height between visibleCells
    for (UITableViewCell *c in visibleCells)
    {
        CGFloat h = c.frame.size.height;
        largestCell = h > largestCell ? h : largestCell;
    }

    // return higher value between screen height and higher visible cell height
    return MAX(largestCell, screenSize);
}

这有效,滚动时不再消失,但我仍然有一个问题:如果我在滚动位置位于大单元格中间时执行 reloadData,它会像之前一样消失......我注意到在重新加载数据后,visibleCells 返回 nil(在我的 heightOfLargestVisibleCell 方法中),所以它采用了 的屏幕高度_visibleBounds 但由于屏幕高度 <到当前可见的单元格高度,这个不显示...

This works, no more disappeared on scrolling but i still have a problem : if i perform reloadData when my scroll position is in the middle of a large cell, it disappear as earlier ... I've noticed that after reloading data, visibleCells return nil (in my heightOfLargestVisibleCell method), so it take my screen height for _visibleBounds but since screen height < to current visible cell height, this one is not displayed ...

有人已经遇到过这个问题吗?

Someone already faced this issue ?

提前谢谢

推荐答案

我为您的问题找到了解决方案.我存储了 heightOfLargestVisibleCell 计算的值,并在重新加载数据后返回最后一个值.

I got a solution for your problem. I store the value calculated by heightOfLargestVisibleCell and return the last value after reload data.

@interface CollectionView : UICollectionView
@property (nonatomic, assign) CGFloat lastLargestCellHeight;
@property (nonatomic, assign) BOOL shouldEvalLargestCellHeight;

@end

@implementation CollectionView

- (CGRect)_visibleBounds
{
    CGRect rect = [super _visibleBounds];
    rect.size.height = [self heightOfLargestVisibleCell];
    return rect;
}


- (CGFloat)heightOfLargestVisibleCell
{
    if (self.shouldEvalLargestCellHeight) {
        // get current screen height depending on orientation
        CGFloat screenSize = self.frame.size.height;

        CGFloat largestCell = 0;

        NSArray *visibleCells = self.visibleCells;

        // get the largest height between visibleCells
        for (UITableViewCell *c in visibleCells)
        {
            CGFloat h = c.frame.size.height;
            largestCell = h > largestCell ? h : largestCell;
        }
        //return higher value between screen height and higher visible cell height
        self.lastLargestCellHeight = MAX(largestCell, screenSize);
        self.shouldEvalLargestCellHeight = NO;
    }
    return self.lastLargestCellHeight;
}

- (void)reloadData
{
    self.shouldEvalLargestCellHeight = NO;
    [super reloadData];
}

- (void)setContentOffset:(CGPoint)contentOffset
{
    self.shouldEvalLargestCellHeight = YES;
    [super setContentOffset:contentOffset];
}

@end

这篇关于滚动时大 UICollectionViewCell 消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆