Large cells in a UICollectionView getting removed

2019-02-03 05:00发布

问题:

I have a UICollectionView that contains some large UICollectionViewCells. These cells are so large that they completely fill the UICollectionView bounds and extend off screen.

The problem is that the large cells are removed from the UICollectionView and queued for reuse while they are still displayed, resulting in a blank view. If I continue scrolling on the blank UICollectionView area, eventually the final portion of the cell appears and the start of the next cell appears in exactly the right place.

I've effectively disabled cell reuse and the problem still occurs apparently because the UICollectionView thinks that the cell is no longer displayed since no corner is within the bounds of the collection view.

To demonstrate make a collection view that is a single column and have a cell that is 10000px tall, when scrolling over the very tall cell it will disappear after about 1000px of content is scrolled off the screen and will reappear to display the final 1000px of content for the cell.

You can download a simple prototype app that displays this problem at: http://jack.cox.s3.amazonaws.com/collectionviewprototype.zip

回答1:

This issue happened for me along with this warning in the debug log:

the behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.

It seems that the out of the box UICollectionViewFlowLayout does not support cells larger than the screen.



回答2:

This issue is being tracked as radar #12889164



回答3:

Try this fix if you are still looking for a quick solution: When a cell is detected to have a bigger height than the collectionview holding it, the collection view simply needs to be larger than the cell. So set the collecitonView frame to be bigger and correct the content and indicator insets.

- (void)updateCollectionViewForLargeCellHeight:(CGFloat)largeCellHeight {
    CGFloat currentCollectionViewHeight = CGRectGetHeight(self.collectionView.frame);
    if (largeCellHeight > currentCollectionViewHeight) {
        self.collectionViewBottomConstraint = -largeCellHeight; 
        //This is the bottom constraint of the collectionView to its superview. 
        //If you are not using constraints, simply set the frame for the collectionView
        UIEdgeInsets edgeInset = UIEdgeInsetsMake(0, 0, largeCellHeight, 0);
        self.collectionView.contentInset = edgeInset;
        self.collectionView.scrollIndicatorInsets = edgeInset;
        [self.collectionView needsUpdateConstraints];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size;
    [self updateCollectionViewForLargeCellHeight:size.height];
    return size;
}


回答4:

I have used UICollectionView with cells of the size of the screen. For iPad the cell size was 768x1024. And haven't found any issues. Please make sure that you have set the Min Spacing For Cells and For Lines are 0 Also return correct CGSize and UIEdgeInsets as follows


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize retval;
    retval.width=maxWidth; //768 in case of iPad
    retval.height=maxHeight; //1024 in case of iPad
    return retval;
}

- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0 , 0, 0, 0);
}