JSQMessagesViewController与SupplementaryView固定在顶部

时间:2016-05-28 13:38:31

标签: ios objective-c uicollectionview jsqmessagesviewcontroller

我重写了collectionView:viewForSupplementaryElementOfKind:atIndexPath:在swift中显示我的JSQMessagesViewController子类的标题视图。这非常有效,但标题视图随着聊天滚动,我希望将其固定在最顶层。

Apple在iOS 9中引入了sectionHeadersPinToVisibleBounds属性,这应该有固定的标题视图,但由于某种原因,这在JSQMessagesViewController子类中不起作用。我已经添加了     self.collectionView.collectionViewLayout.sectionHeadersPinToVisibleBounds = true to viewDidLoad()

我尝试的另一个解决方案(来自这篇文章How to make Supplementary View float in UICollectionView as Section Headers do in UITableView plain style)是子类JSQMessagesCollectionViewFlowLayout,这几乎起作用。向上滑动时,标题视图保持固定在顶部,聊天消息将在标题视图下推送。但是一旦我尝试向下滑动,当应用程序崩溃时,我在控制台中收到以下错误:

*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.12/UICollectionViewData.m:408
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'layout attributes for supplementary item at index path (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}) changed from <JSQMessagesCollectionViewLayoutAttributes: 0x13f65a730> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 414 100); zIndex = 10;  to <JSQMessagesCollectionViewLayoutAttributes: 0x13f679920> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 10; 414 100); zIndex = 1024;  without invalidating the layout'

有没有人以相同的方式为JSQMessagesViewController修复标题视图?

我改变的方法是     shouldInvalidateLayoutForBoundsChange: 在哪里我回来了,

和layoutAttributesForElementsInRect:我改为:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *attributesInRect = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

//    if (self.springinessEnabled) {
//        NSMutableArray *attributesInRectCopy = [attributesInRect mutableCopy];
//        NSArray *dynamicAttributes = [self.dynamicAnimator itemsInRect:rect];
//        
//        //  avoid duplicate attributes
//        //  use dynamic animator attribute item instead of regular item, if it exists
//        for (UICollectionViewLayoutAttributes *eachItem in attributesInRect) {
//            
//            for (UICollectionViewLayoutAttributes *eachDynamicItem in dynamicAttributes) {
//                if ([eachItem.indexPath isEqual:eachDynamicItem.indexPath]
//                    && eachItem.representedElementCategory == eachDynamicItem.representedElementCategory) {
//                    
//                    [attributesInRectCopy removeObject:eachItem];
//                    [attributesInRectCopy addObject:eachDynamicItem];
//                    continue;
//                }
//            }
//        }
//        
//        attributesInRect = attributesInRectCopy;
//    }
    UICollectionView * const cv = self.collectionView;
    CGPoint const contentOffset = cv.contentOffset;
    NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];


    for (UICollectionViewLayoutAttributes *layoutAttributes in attributesInRect) {
        if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
            [missingSections removeIndex:layoutAttributes.indexPath.section];
        }
    }

    [attributesInRect enumerateObjectsUsingBlock:^(JSQMessagesCollectionViewLayoutAttributes *attributesItem, NSUInteger idx, BOOL *stop) {
        if (attributesItem.representedElementCategory == UICollectionElementCategoryCell) {
            [self jsq_configureMessageCellLayoutAttributes:attributesItem];
            [missingSections addIndex:attributesItem.indexPath.section];
        }
        else {
            attributesItem.zIndex = -1;
        }
    }];

    [missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];
        UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
        [attributesInRect addObject:layoutAttributes];
    }];

    for (UICollectionViewLayoutAttributes *layoutAttributes in attributesInRect)  {
        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];
            NSIndexPath *firstObjectIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
            NSIndexPath *lastObjectIndexPath = [NSIndexPath indexPathForItem:MAX(0, (numberOfItemsInSection - 1)) inSection:section];
            UICollectionViewLayoutAttributes *firstObjectAttrs;
            UICollectionViewLayoutAttributes *lastObjectAttrs;
            if (numberOfItemsInSection > 0) {
                firstObjectAttrs = [self layoutAttributesForItemAtIndexPath:firstObjectIndexPath];
                lastObjectAttrs = [self layoutAttributesForItemAtIndexPath:lastObjectIndexPath];

            } else {
                firstObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:firstObjectIndexPath];
                lastObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:lastObjectIndexPath];
            }
            CGFloat headerHeight = CGRectGetHeight(layoutAttributes.frame);
            CGPoint origin = layoutAttributes.frame.origin;
            origin.y = MIN(MAX(contentOffset.y + cv.contentInset.top, (CGRectGetMinY(firstObjectAttrs.frame) - headerHeight)), (CGRectGetMaxY(lastObjectAttrs.frame) - headerHeight));
            layoutAttributes.zIndex = 1024;
            layoutAttributes.frame = (CGRect) {
                .origin = origin, .size = layoutAttributes.frame.size
            };
        }
    }



    return attributesInRect;
}

1 个答案:

答案 0 :(得分:0)

我上面提到的代码没问题。我认为导致崩溃的原因是因为我覆盖了.simba.cassandraodbc.ini子类中的collectionView:collectionViewLayout:referenceSizeForHeaderInSection:函数。我注释掉了这个函数,而是在JSQMessagesViewController中将kJSQMessagesLoadEarlierHeaderViewHeight常量设置为常量。是的,我劫持了LoadEarlierHeaderView以显示我自己的自定义标题视图。

现在,标题视图固定在顶部,显示我的自定义标题视图,并且没有任何崩溃。