0

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

我已经实现了@JonathanCichon 解决方案,它是 UICollectionView 的子类并执行自定义操作_visibleBounds(我知道这是一个私有 API,但无论如何,我不需要在 Apple Store 上提交它)

这是我的自定义收藏视图:

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

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

提前谢谢

4

1 回答 1

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
于 2013-07-15T22:54:29.077 回答