Large UICollectionViewCell stopped being displayed when scrolling

前端 未结 4 546
夕颜
夕颜 2020-12-25 14:21

The same behavior of UICollectionView as described here has been led to this question. Even though I decided to post my own one, because I did further investiga

相关标签:
4条回答
  • 2020-12-25 14:39

    My solution is basically the same as Jonathan's but in a category, so you don't have to use your own subclass.

    @implementation UICollectionView (MTDFixDisappearingCellBug)
    
    + (void)load {
        NSError *error = nil;
        NSString *visibleBoundsSelector = [NSString stringWithFormat:@"%@isib%@unds", @"_v",@"leBo"];
    
        if (![[self class] swizzleMethod:NSSelectorFromString(visibleBoundsSelector) withMethod:@selector(mtd_visibleBounds) error:&error]) {
            FKLogErrorVariables(error);
        }
    }
    
    - (CGRect)mtd_visibleBounds {
        CGRect bounds = [self mtd_visibleBounds]; // swizzled, no infinite loop
        MTDDiscussCollectionViewLayout *layout = [MTDDiscussCollectionViewLayout castedObjectOrNil:self.collectionViewLayout];
    
        // Don`t ask me why, but there's a visual glitch when the collection view is scrolled to the top and the max height is too big,
        // this fixes it
        if (bounds.origin.y <= 0.f) {
            return bounds;
        }
    
        bounds.size.height = MAX(bounds.size.height, layout.maxColumnHeight);
    
        return bounds;
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-25 14:51

    I found that this issue only occurred when using a subclassed UICollectionViewLayoutAttributes and when that attribute class did not have a correct isEqual: method.

    So for example:

    @implementation COGridCollectionViewLayoutAttributes
    - (id)copyWithZone:(NSZone *)zone
    {
        COGridCollectionViewLayoutAttributes *attributes = [super copyWithZone:zone];
        attributes.isInEditMode = _isInEditMode;
        return attributes;
    }
    
    - (BOOL)isEqual:(id)other {
        if (other == self) {
            return YES;
        }
        if (!other || ![[other class] isEqual:[self class]]) {
            return NO;
        }
        if ([((COGridCollectionViewLayoutAttributes *) other) isInEditMode] != [self isInEditMode]) {
            return NO;
        }
    
        return [super isEqual:other];
    }
    
    @end
    

    Worked but originally I had:

    return YES;
    

    This is on iOS 7.

    0 讨论(0)
  • 2020-12-25 14:52

    i have a VERY dirty and internal solution for this problem:

    @interface UICollectionView ()
    - (CGRect)_visibleBounds;
    @end
    
    @interface MyCollectionView : UICollectionView
    
    @end
    
    @implementation MyCollectionView
    
    - (CGRect)_visibleBounds {
        CGRect rect = [super _visibleBounds];
        rect.size.height = [self heightOfLargestVisibleCell];
        return rect;
    }
    
    - (float)heightOfLargestVisibleCell {
        // do your calculations for current max cellHeight and return it 
        return 1234;
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-25 14:58

    I have a workaround that seems to be working for me and should not run amok of Apple's rules for iOS applications.

    The key is the observation that the large cells bounds are the issue. I've worked around that by ensuring that one edge of the cell is within the viewable area of the scrollable content region. You'll obviously need to subclass the UICollectionViewFlowLayout class or UICollectionViewLayout depending on your needs and make use of the contentOffset value to track where you are in the UIScrollView.

    I also had to ensure:

    - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds 
    

    returns YES or face a runtime exception indicating the layout was invalid. I keep the edge of the larger cell bound to the left edge in my case. This way you can avoid the erroneous bounds intersection detection for these larger cells.

    This does create more work depending on how you would like the contents of the cell to be rendered as the width/height of the cell is being updated as you scroll. In my case, the subviews within the cell are relatively simple and do not require a lot of fiddling with.

    As requested here is an example of my layoutAttributesInRect

    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
        NSMutableArray* attributes = [NSMutableArray array];
        NSArray *vertical = myVerticalCellsStore.cells;
        NSInteger startRow = floor(rect.origin.y * (vertical.count)/ (vertical.count * verticalViewHeight + verticalViewSpacing * 2));
        startRow = (startRow < 0) ? 0 : startRow;
    
        for (NSInteger i = startRow; i < vertical.count && (rect.origin.y + rect.size.height >= i * verticalViewHeight); i++) {
            NSArray *horizontals = myHorizontalStore.horizontalCells;
            UICollectionViewLayoutAttributes *verticalAttr = [self layoutAttributesForSupplementaryViewOfKind:@"vertical" atIndexPath:[NSIndexPath indexPathForItem:0 inSection:i]];
            if (CGRectIntersectsRect(verticalAttr.frame, rect)) {
                [attributes addObject:verticalAttr];
            }
    
            BOOL foundAnElement = NO;
            for (NSInteger j = 0 ; j < horizontals.count; j++) {
                MYViewLayoutAttributes *attr = (MyViewLayoutAttributes *)[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:j inSection:i]];
                if (CGRectIntersectsRect(rect, attr.frame)) {
                    [attributes addObject: attr];
                    foundAnElement = YES;
                }
                else if (foundAnElement) {
                    break;
                }
            }
        }
        return attributes;
    }
    

    This is my sanitized code. Basically I calculate about were the first cell should be based on the cell height. In my case that is fixed, so the calculation is pretty easy. But my horizontal elements have various widths. So the inner loop is really about figuring out the right number of horizontal cells to include in the attributes array. There I'm using the CGRectIntersectsRect to determine if the cell intersects. Then the loop keeps going until the intersection fails. And if at least one horizontal cell has been found the loop will break. Hope that helps.

    0 讨论(0)
提交回复
热议问题