Ios UICollectionView页眉和页脚的行为非常奇怪
我正在UIViewController中使用基类UICollectionViewFlowLayout实现基类UICollectionView。我的控制器是集合视图委托和数据源。只有项目视图和补充视图是自定义类,没有NIB。据我所知,一切都是标准实现。我尽可能地把它保留下来 HeaderView是UICollectionReusableView的一个子类 这是我的收藏视图创建Ios UICollectionView页眉和页脚的行为非常奇怪,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我正在UIViewController中使用基类UICollectionViewFlowLayout实现基类UICollectionView。我的控制器是集合视图委托和数据源。只有项目视图和补充视图是自定义类,没有NIB。据我所知,一切都是标准实现。我尽可能地把它保留下来 HeaderView是UICollectionReusableView的一个子类 这是我的收藏视图创建 -(void) viewDidLoad { [super viewDidLoad]; UICollec
-(void) viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.headerReferenceSize = CGSizeMake(0, 50);
flow.footerReferenceSize = CGSizeMake(0, 50);
flow.itemSize = CGSizeMake(100, 150);
flow.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
[flow setScrollDirection:UICollectionViewScrollDirectionVertical];
cellID = @"CollectionCellIdentifier";
headerID = @"header";
footerID = @"footer";
collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height-70) collectionViewLayout:flow];
[collection registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:cellID];
[collection registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
[collection registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerID];
[collection setDelegate:self];
[collection setDataSource:self];
[self.view addSubview:collection];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
HeaderView *header;
NSString *suppID = headerID;
if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
suppID = footerID;
}
header = (HeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:suppID forIndexPath:indexPath];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
if (indexPath.section == kInSection) {
header.label.text = @"In Groups";
}
else {
header.label.text = @"Other Groups";
}
}
else {
header.label.text = @"";
}
return header;
}
下面是我的补充视图创建
-(void) viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.headerReferenceSize = CGSizeMake(0, 50);
flow.footerReferenceSize = CGSizeMake(0, 50);
flow.itemSize = CGSizeMake(100, 150);
flow.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
[flow setScrollDirection:UICollectionViewScrollDirectionVertical];
cellID = @"CollectionCellIdentifier";
headerID = @"header";
footerID = @"footer";
collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height-70) collectionViewLayout:flow];
[collection registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:cellID];
[collection registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
[collection registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerID];
[collection setDelegate:self];
[collection setDataSource:self];
[self.view addSubview:collection];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
HeaderView *header;
NSString *suppID = headerID;
if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
suppID = footerID;
}
header = (HeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:suppID forIndexPath:indexPath];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
if (indexPath.section == kInSection) {
header.label.text = @"In Groups";
}
else {
header.label.text = @"Other Groups";
}
}
else {
header.label.text = @"";
}
return header;
}
当我运行此操作时,项目看起来很好。第一节的标题(kInSection)显示良好。第一节底部没有页脚。第二节(kOutSection)顶部没有标题。这些页脚和页眉在第二节下面。第二节标题显示的文本正确无误
最奇怪的是,当我将集合视图向上滚动足够远时,我可以看到第二部分下方的页眉和页脚。。。当我滚动经过它们应该渲染的位置时,它们会从屏幕中间消失。这真的很奇怪
否则,此集合视图工作正常。我可以与项目交互,我可以插入和删除项目,等等
UICollectionView是否不可靠?我真的做错什么了吗?以前有人经历过类似的事情吗?在我通常可靠的iOS开发研究中,我没有发现任何东西
如果有人需要任何其他信息,请告诉我。但实际上,这是我在研究中收集到的基本实现