大UICollectionViewCell消失滚动(Large UICollectionViewCe

2019-10-30 09:59发布

我为遇到同样的问题, 在这里它是大UICollectionViewCell(超过UICollectionView高度的两倍显然)在给定的滚动偏移消失,然后给定的滚动也抵消后再次出现。

我实现了@JonathanCichon解决方案这是继承UICollectionView和执行上的自定义操作_visibleBounds (我知道这是一个私人API,但不管,我并不需要提交它在苹果商店)

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

#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但由于屏幕高度<当前可见单元格高度,则不会显示这一个...

有人已经面临这个问题?

Thx提前

Answer 1:

我得到了你的问题的解决方案。 我存储由heightOfLargestVisibleCell计算的价值和回报重装数据后的最后一个值。

@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


文章来源: Large UICollectionViewCell disappear on scrolling