【问题标题】:UICollectionView Header xib not showingUICollectionView Header xib 未显示
【发布时间】:2015-08-15 12:29:27
【问题描述】:

我有一个collectionview,我想使用xib 作为标题。但是,xib 不会出现。首先,我尝试向 xib 添加标签,但没有出现。然后我将整个背景颜色设置为红色。它没有出现。 collectionview 项目为标题留下一个空白,但它是完全空白的。类似的 SO 线程是 thisthis,但由于我没有使用情节提要并且我的标题高度大于零,所以它们不能解决我的问题。这是我所有的相关代码:

#import "ScoreboardCollectionViewController.h"
#import "ScoreboardCollectionViewCell.h"
#import "ScoreboardReusableView.h"
#import "ScoreboardModel.h"
#import "Scoreboard.h"

...

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register cell classes
    UINib *cellNib = [UINib nibWithNibName:@"ScoreboardCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cell"];

    [self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"scoreboardHeader"];


    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark_fish_skin_"]]; 
}

...

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 34);
}

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

    ScoreboardReusableView *view = [[ScoreboardReusableView alloc] init];

    view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                              withReuseIdentifier:@"scoreboardHeader"
                                                     forIndexPath:indexPath];

    return view;
}

这是我的 xib 的屏幕截图:

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionreusableview


    【解决方案1】:

    好的,我解决了。问题是在我的viewDidLoad 方法中我有

    [self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"scoreboardHeader"];
    

    自从我使用笔尖以来,它应该是:

    [self.collectionView registerNib:[UINib nibWithNibName:@"ScoreboardReusableView" bundle:nil]
              forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                     withReuseIdentifier:@"scoreboardHeader"];
    

    【讨论】:

    • 谢谢!我有同样的问题,这帮助我解决了我的问题。即使在我发现你的这篇文章之后,我也没有意识到我也有一个错字,这让我很心痛。但那是我的错。
    【解决方案2】:

    我缺少要启用的 Collection View 的“Section Header”选项:-

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 2021-01-21
      • 2016-05-14
      相关资源
      最近更新 更多