UICollectionView的头视图未能正确地出列

4

我正在尝试为UICollectionView添加一个头部。我使用的是自定义布局,可以横向滚动,用于查看朋友列表中的头像图像。我可以让头部出现,但无法重用。一旦头部视图离开屏幕,它就永远不会重新加载。有人能想出原因吗?

谢谢!

集合视图数据源:

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

    if (self.showAddAvatarHeaderView && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
        return [collectionView dequeueAddAvatarViewHeaderForIndexPath:indexPath];
    }

    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(SWAvatarViewerCollectionViewFlowLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (!self.showAddAvatarHeaderView) {
        return CGSizeZero;
    }

    return CGSizeMake(kSWAvatarViewerAddAvatarHeaderViewWidth, CGRectGetHeight(collectionView.bounds));
}

头像集合视图:

- (SWAvatarViewerAddAvatarHeaderView *)dequeueAddAvatarViewHeaderForIndexPath:(NSIndexPath *)indexPath {
    SWAvatarViewerAddAvatarHeaderView *headerView = [super dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                             withReuseIdentifier:[SWAvatarViewerAddAvatarHeaderView headerReuseIdentifier]
                                                                                    forIndexPath:indexPath];
    headerView.delegate = self;
    return headerView;
}

Nib文件注册:

  [self registerNib:[SWAvatarViewerAddAvatarHeaderView nib]
        forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
        withReuseIdentifier:[SWAvatarViewerAddAvatarHeaderView headerReuseIdentifier]];

布局:

#pragma mark - Initialization

- (void)configureFlowLayout {
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // Padding for cells is taken into account in the cell's layout. Remove all
    // padding between cells
    self.sectionInset = UIEdgeInsetsMake(0, 00.0f, 0, 00.0f);
    self.minimumLineSpacing = 0.0f;
    self.minimumInteritemSpacing = CGFLOAT_MAX;

    _cellBottomLabelFont = [UIFont systemFontOfSize:12.0];

    CGSize defaultAvatarSize = CGSizeMake(44.0f, 44.0f);
    _avatarViewSize = defaultAvatarSize;

    _springinessEnabled = YES;
    _springResistanceFactor = 1000;
}

这与“dequeue”无关。如果标题视图出现,它会被出列(或者至少看起来是这样)。如果它在滚动屏幕后消失,那就与NSCollectionViewLayout有关。但是你没有提到任何相关信息,因此你没有提供任何相关信息。 - matt
那么它并不是自定义布局?它只是普通的内置流式布局? - matt
这只是一个微调过的流式布局,其中加入了一些UIKitDynamics来在单元格之间添加弹簧效果,但本质上它仍然是一个水平流式布局。 - DBoyer
你在自定义布局中覆盖了prepareLayout方法吗?如果没有,你可能需要这样做,并确保你的头部视图有正确的框架。 - Lev Landau
3个回答

1

只需要使用这个方法,就可以解决所有问题。

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(-50, 10, 10, 10); //asuming

    //UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
} 

享受。

1
你似乎正在使用我从未听说过的第三方布局。经过与你的讨论,我的感觉是我在你的问题下发表的第一条评论可能是正确的:布局本身可能存在缺陷。
在集合视图中,单元格的布局属性(位置、大小、变换、透明度等)是布局的责任。因此,如果某些东西仅仅因为被滚动到屏幕外再次滚动回来而消失,那么看起来布局本身没有正确地完成其工作。

这是我编写的一个库。布局和其中所有的漏洞都是我自己制造的 :P - DBoyer
2
那么你完全问错了问题。而且两天前我就要求你展示你的布局代码,但你仍未提供足够的信息让任何人能帮助你。 - matt

1
快速谷歌搜索并没有揭示出SWAvatarViewerCollectionViewFlowLayout。如果您有源代码,可以查看布局代码,那里应该有一个名为layoutAttributesForElementsInRect:的方法。
集合视图会在项目离开屏幕时立即将其出队列,这是通过上述方法确定的(布局属性包含视图的中心和尺寸)。如果该方法始终返回标题的布局属性,则不会将其出队列。
但是,如果您无法访问代码(例如静态库),则可能无法做太多事情。

我自己编写了布局。我会再次检查layoutAttributesForElementsInRect:方法。谢谢。 - DBoyer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接