【问题标题】:UICollectionView header view not dequeueing properlyUICollectionView 标题视图未正确出列
【发布时间】:2014-09-23 23:29:57
【问题描述】:

我正在尝试向集合视图添加标题。我正在使用水平滚动的自定义布局,它用于查看朋友的头像图像列表。我可以让标题出现,但它不会出队。一旦标题视图离开屏幕,它就永远消失了。谁能弄清楚这是为什么?

谢谢!

集合查看数据源:

- (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;
}

【问题讨论】:

  • 这与“出队”无关。如果出现标题视图,它将出列(或至少出现)。如果它在滚动到屏幕外后消失,则与 NSCollectionViewLayout 有关。但是你没有说那是什么,所以你没有提供任何相关信息。
  • 所以它毕竟不是自定义布局?只是普通的内置流式布局?
  • 它只是一个调整过的流布局,带有一些 UIKitDynamics 以在单元格之间添加弹簧,但它本质上是一个水平流布局。
  • 您是否在自定义布局中覆盖 prepareLayout?如果没有,您可能需要这样做并确保您的标题视图具有正确的框架。

标签: ios objective-c xcode ios7 uicollectionview


【解决方案1】:

您显然在使用我从未听说过的第三方布局。在与您讨论后,我的感觉是我在您的问题下的第一条评论可能是正确的:布局本身可能有问题。

在集合视图中,单元格的布局属性(位置、大小、变换、alpha 等)是布局的责任。因此,如果某些东西仅仅因为它从屏幕上滚动出来然后又重新打开而消失,那么听起来布局本身并没有正确地完成它的工作。

【讨论】:

  • 这是我自己写的一个库。布局及其所有错误都是我制作的:P
  • 那你完全问错问题了。而且你还没有显示你的布局代码,正如我在两天前要求的那样。您没有为任何人提供足够的信息来帮助您。
【解决方案2】:

快速谷歌搜索没有发现 SWAvatarViewerCollectionViewFlowLayout。有源码的可以看一下布局代码,应该有一个方法叫layoutAttributesForElementsInRect:

集合视图在项目离开屏幕后立即将它们出列,这是在上述方法的帮助下确定的(布局属性包含视图中心和尺寸)。如果该方法总是返回标题的布局属性,它将不会出队。

但是,如果您无权访问代码(如静态库),您可能无能为力。

【讨论】:

  • 我自己写了布局。我将再次检查 layoutAttributesForElementsInRect: 方法。谢谢
【解决方案3】:

只要放这个方法就可以解决所有问题

- (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#>)
} 

享受吧。

【讨论】:

    猜你喜欢
    • 2016-09-19
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多