UICollectionView和Supplementary View(标题) [英] UICollectionView and Supplementary View (header)
问题描述
尝试在我的 UICollectionView
中添加一个补充视图作为标题。我有问题让它工作。
Trying to add a a Supplementary view into my UICollectionView
as a header. I'm having issues getting it to work.
我使用自定义 UICollectionViewFlowLayout
返回 contentSize
总是至少比帧大1个像素(我使用的是 UIFreshControl
,只有在<$ c $时才有效c> UICollectionView 滚动,并设置 collectionView.contentSize
直接无效)和 invalidateLayout
在 sectionInsert
和 itemSize
更改:
I use a custom UICollectionViewFlowLayout
to return a contentSize
that is always at least 1 pixel larger then the frame (I am using a UIFreshControl
which will only work if the UICollectionView
scrolls, and setting collectionView.contentSize
directly does nothing) and to invalidateLayout
on sectionInsert
and itemSize
changes:
-(void)setSectionInset:(UIEdgeInsets)sectionInset {
if (UIEdgeInsetsEqualToEdgeInsets(super.sectionInset, sectionInset)) {
return;
}
super.sectionInset = sectionInset;
[self invalidateLayout];
}
-(void) setItemSize:(CGSize)itemSize {
if (CGSizeEqualToSize(super.itemSize, itemSize)) {
return;
}
super.itemSize = itemSize;
[self invalidateLayout];
}
- (CGSize)collectionViewContentSize
{
CGFloat height = [super collectionViewContentSize].height;
// Always returns a contentSize larger then frame so it can scroll and UIRefreshControl will work
if (height < self.collectionView.bounds.size.height) {
height = self.collectionView.bounds.size.height + 1;
}
return CGSizeMake([super collectionViewContentSize].width, height);
}
我创建了一个 UICollectionReusableView
类只是一个 UIView
,带有 UILabel
:
I created a UICollectionReusableView
class which is just a UIView
with a UILabel
:
@implementation CollectionHeaderView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionHeaderView" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
self.headerLabel.text = @"This is a header. There are many like it.";
self.backgroundColor = [UIColor yellowColor];
}
return self;
}
尝试实施它:
DatasetLayout *collectionViewFlowLayout = [[DatasetLayout alloc] init];
collectionViewFlowLayout.itemSize = CGSizeMake(360, 160);
collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(16, 16, 16, 16);
collectionViewFlowLayout.minimumInteritemSpacing = 16;
collectionViewFlowLayout.minimumLineSpacing = 16;
collectionViewFlowLayout.headerReferenceSize = CGSizeMake(0, 100);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewFlowLayout];
collectionView.translatesAutoresizingMaskIntoConstraints = FALSE;
collectionView.backgroundColor = [UIColor yellowColor];
collectionView.delegate = self;
collectionView.dataSource = self;
我注册课程:
[self.collectionView registerClass:[CollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView"];
并实施代表:
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
CollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView" forIndexPath:indexPath];
headerView.headerLabel.text = @"Blarg!";
return headerView;
}
该行
collectionViewFlowLayout.headerReferenceSize = CGSizeMake(0, 100);
导致错误:
*** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:], /SourceCache/UIKit_Sim/UIKit-2380.17/UICollectionView.m:1150
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
如果我发表评论,它但没有标题。
If I comment it out, it runs but no header.
我做错了什么或者没有实现?
What am I doing wrong or not implementing?
推荐答案
我遇到了类似的问题,但是在我以编程方式弹出我的UICollectionViewController后,我的应用程序崩溃了。在某些原因(我认为它只是SDK中的一个错误)self.collectionView在其'控制器销毁后仍然存在,从而导致此失败:
I faced with the similar problem, but my app crashed after I programmatically pop my UICollectionViewController. In some reason (I think it's just a bug in SDK) self.collectionView was alive after its' controller destroy, thus causing this failure:
*** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:1305
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
解决方案是在UICollectionViewController中覆盖-dealloc并手动释放self.collectionView。 ARC代码:
The solution is just override -dealloc in UICollectionViewController and release self.collectionView manually. ARC code:
- (void)dealloc {
self.collectionView = nil;
}
希望这会为某人节省时间。
Hope this will save time for somebody.
这篇关于UICollectionView和Supplementary View(标题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!