我希望我在UICollectionView中的部分有一个带有图像的标题。
我遵循了以下步骤:
UICollectionView分配了一个标题作为附件UICollectionReusableView子类。ImageViewImageView文件中的自定义类中为.h设置了一个出口viewDidLoad上实现了以下功能[self.collectionView registerClass:[ScheduleHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader)
{
ScheduleHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
headerView.headerImageView.image = [UIImage imageNamed:@"blah.png"];
reusableview = headerView;
}
return reusableview;
}我知道数据源和委托方法是有效的,因为我可以看到所有的单元格及其部分。然而,我没有我的标题。我在上面的方法中输入了一个断点,而且它从未被调用过。
我做错什么了?
发布于 2013-08-11 16:41:59
似乎你必须给你的标题一个非零的大小,否则collectionView:viewForSupplementaryElementOfKind:atIndexPath就不会被调用。因此,可以设置流布局的headerReferenceSize属性,如下所示:
flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.frame.size.width, 100.f);Swift 5+
flowLayout.headerReferenceSize = CGSize(CGSize(width: self.collectionView.frame.size.width, height: 100))或者,如果希望按节更改大小,请实现collectionView:layout:referenceSizeForHeaderInSection。
发布于 2014-06-18 22:19:03
你回答了这个问题,但用我的话来说,我更明白:
若要调用该方法,请添加以下方法:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(60.0f, 30.0f);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
return CGSizeMake(60.0f, 30.0f);
}...and从那里出发。
发布于 2017-10-12 21:44:41
Swift 4 Xcode 9.1 Beta 2
加@objc
@objc func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize{
let size = CGSize(width: 400, height: 50)
return size
}学分:https://medium.com/@zonble/your-delegation-methods-might-not-be-called-in-swift-3-c6065ed7b4cd
https://stackoverflow.com/questions/18173191
复制相似问题