2017-07-20 30 views
2

我已將<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>添加到我的收藏視圖中,並設置了我的UICollectionView,如下所示。UICollectionReusableView未顯示

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerReuseIdentifier]; 

我也添加了必要的方法。

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

- (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"supplementaryViewForElementKind called"); 
    if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) { 
     UICollectionReusableView *header = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                       withReuseIdentifier:headerReuseIdentifier 
                         forIndexPath:indexPath]; 
     header.backgroundColor = [UIColor redColor]; 
     return header; 
    } 

    return nil; 
} 

但是,supplementaryViewForElementKind永遠不會被調用。有任何想法嗎?

+0

您是否在viewdidload中添加了self.collectionView.delegate = self? – StefanS

+0

是的,對不起。我忘了在這個問題上加上這個。 – user4992124

+0

你註冊到實現uicollectionviewdelegate中的代表嗎? – StefanS

回答

0

如果您已在Interface Builder中添加了標題視圖並已分配了該類,那麼您應該刪除代碼中的registerClass調用。它應該顯示。在你的委託方法中休息一下,看看它被調用。