UICollectionView部分标题闪烁问题

top*_*ide 4 objective-c ios uicollectionview uicollectionviewlayout ios7

这个帖子(也许是愚蠢的)经过连续8个小时对我的计算机像一些愚蠢的猴子一样g ,,因为UICollectionView在iOS 7上存在问题.

我有子类UICollectionViewFlowLayout为我的UICollectionView dataSource的所有50个部分提供了一个通用的"Section X"(Sticky Header)[其中X是一个数字]标题,这是一个NSArray分区.

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {在我的子类中调用,它确实显示了标题,但仅适用于第一部分.这是非常奇怪的行为.我的期望是:

|S| [CELL] [CELL] |S| [CELL] [CELL] 
|E| [    ] [    ] |E| [    ] [    ] 
|C| [____] [____] |C| [____] [____] 
|T|               |T|
| | [CELL] [CELL] | | [CELL] 
| | [    ] [    ] | | [    ] 
|1| [____] [____] |2| [____]
Run Code Online (Sandbox Code Playgroud)

"SECT 2"是第一个之后的下一个标题,当用户水平滚动时(应用于垂直方向)应该在屏幕上可见,但我得到的是:

|S| [CELL] [CELL] | | [CELL] [CELL] 
|E| [    ] [    ] | | [    ] [    ] 
|C| [____] [____] | | [____] [____] 
|T|               | |
| | [CELL] [CELL] | | [CELL] 
| | [    ] [    ] | | [    ] 
|2| [____] [____] | | [____]
Run Code Online (Sandbox Code Playgroud)

第一个之后的空白部分标题,以及第二个部分标题位于错误的位置.这一切的变化,当我滚动UICollectionView.在错误的地方的第二个消失了,但它没有去它应该的位置.

稍微滚动之后:

|S| [CELL] [CELL] | | [CELL] [CELL] 
|E| [    ] [    ] | | [    ] [    ] 
|C| [____] [____] | | [____] [____] 
|T|               | |
| | [CELL] [CELL] | | [CELL] 
| | [    ] [    ] | | [    ] 
|1| [____] [____] | | [____]
Run Code Online (Sandbox Code Playgroud)

仍然是第1节之后的空白.

UICollectionView实际滚动到该部分的开头之前,没有任何部分标题实际上是可见的.它不是滚动到位,而是显示在正确的位置,没有任何动画.我继续滚动,这是相同的行为.

我已经在没有UICollectionViewFlowLayout子类的情况下测试了这种行为,只是UIView在屏幕上粘贴了一个泛型.在iOS 6上,这个问题不存在.标题似乎在整个滚动过程中出列并正确显示,没有任何丝毫问题.

任何人都可以帮助我,或者UICollectionView在iOS 7上提供章节标题时是否有其他人遇到此问题?我失去了理智!

layoutAttributesForElementsInRect: 方法

    - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {

    NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    UICollectionView * const cv = self.collectionView;
    CGPoint const contentOffset = cv.contentOffset;

    NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
    for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
        if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
            [missingSections addIndex:layoutAttributes.indexPath.section];
        }
    }
    for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
        if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
            [missingSections removeIndex:layoutAttributes.indexPath.section];
        }
    }

    [missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {

        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];

        UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];

        [answer addObject:layoutAttributes];

    }];

    for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {

        if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {

            NSInteger section = layoutAttributes.indexPath.section;
            NSInteger numberOfItemsInSection = [cv numberOfItemsInSection:section];

            NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
            NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:MAX(0, (numberOfItemsInSection - 1)) inSection:section];

            UICollectionViewLayoutAttributes *firstCellAttrs = [self layoutAttributesForItemAtIndexPath:firstCellIndexPath];
            UICollectionViewLayoutAttributes *lastCellAttrs = [self layoutAttributesForItemAtIndexPath:lastCellIndexPath];

            CGFloat headerWidth = CGRectGetWidth(layoutAttributes.frame);
            CGPoint origin = layoutAttributes.frame.origin;
            origin.x = MIN(MAX(contentOffset.x, (CGRectGetMinX(firstCellAttrs.frame) - headerWidth)),
                           (CGRectGetMaxX(lastCellAttrs.frame) - headerWidth)
                           );

            layoutAttributes.zIndex = 1024;
            layoutAttributes.frame = (CGRect){
                .origin = origin,
                .size = layoutAttributes.frame.size
            };

        }

    }

    return answer;

}
Run Code Online (Sandbox Code Playgroud)

viewForSupplementaryElementOfKind: 相关代码:

CollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:CellIdentifier forIndexPath:indexPath];

        headerView.frame = CGRectMake(0, 0, 60, sectionContainerView.frame.size.height);
[headerView setBackgroundColor:[UIColor greenColor]]; //to test visibility
return headerView;
Run Code Online (Sandbox Code Playgroud)

top*_*ide 8

我解决了我的问题.事实证明,在iOS 7里面

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

不能设置你的补充视图的框架.留下适当的委托方法.

我的天啊.17个小时后,我要去睡觉了.