ios - UICollectionView header xib 未显示

标签 ios objective-c uicollectionview uicollectionreusableview

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

#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 的截图: enter image description here

最佳答案

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

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

当我使用 Nib 时,它应该是:

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

关于ios - UICollectionView header xib 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583796/

相关文章:

ios - 在 UISearchDisplayController 中选择单元格后滚动到 UITableView 中选定的单元格?

ios - 点击 UITextField 后跟 UIDatePicker 时,UITableView 向上滚动两次

ios - 将不同的数据和不同长度的项目分配给 UITableView 内的 UICollectionView

ios - swift 错误 : Struct 'XX' must be completely initialized before a member is stored to

ios - 如何从控制中心控制向前和向后搜索?

ios - 在部分代码中抑制 "is partial: introduced in iOS"

ios - 带有自定义标题的 UICollectionView

ios - 搜索栏 - 过滤 Collection View 数据源的搜索词数组

php - XMPP 流发送,不接收。为什么 XMPP 流会断开连接?

ios - 自定义 View Controller 转换偶尔崩溃