Gif*_*ius 30 objective-c ios uicollectionview uicollectionreusableview
下面的代码正确显示我的标题视图,但是对于UICollectionView中的每个部分:
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView * headerView =
[collectionView
dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"SectionHeaderCollectionReusableView"
forIndexPath:indexPath];
switch (indexPath.section) {
case Section_One:
return headerView;
case Section_Two:
return headerView;
case Section_Three:
return headerView;
case Section_Four:
return headerView;
case Section_Five:
return headerView;
default:
return headerView;
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是,不显示'Section_One'或'Section_Two'的标题视图,但返回'nil'会导致'NSInternalInconsistencyException':
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView * headerView =
[collectionView
dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"SectionHeaderCollectionReusableView"
forIndexPath:indexPath];
switch (indexPath.section) {
case Section_One:
return nil;
case Section_Two:
return nil;
case Section_Three:
return headerView;
case Section_Four:
return headerView;
case Section_Five:
return headerView;
default:
return nil;
}
}
Run Code Online (Sandbox Code Playgroud)
如何仅显示某些部分的标题视图?
mwr*_*ght 56
继续并为每个部分返回一个标题,然后在此UICollectionViewDelegate函数中将节标题的大小设置为大小为零.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return CGSizeZero;
}else {
return CGSizeMake(self.collectionView.bounds.size.width, desiredHeight);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个UICollectionViewController控制两个UICollectionViews的情况(后面称为集合视图1和2)我想要第一个标题和第二个没有标题(或页脚).
什么是从@ mwright的答案缺少的是,当您返回CGSizeZero的集合视图如下:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if collectionView == self.collectionView2 {
return CGSizeZero
}
return < something else >
}
Run Code Online (Sandbox Code Playgroud)
...表示collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView 根本没有为集合视图2调用.
这意味着您无需担心为第二个集合视图返回"错误"标头是徒劳的.
我注意到当您在 XIB 中为可重用标头使用 AutoLayout 时,接受的答案中断了。
如果您将内容与边缘隔开或将标题视图中的项目设置为静态的、不可变的大小,则它尤其会被破坏。将标题大小设置为 CGSizeZero 使我的调试器控制台变得混乱,来自 Interface Builder 的数十个警告说它们会破坏所有约束以满足委托方法中设置的要求。
虽然这本身在技术上并不是一场灾难,但它仍然很脏。在 Swift 和 AutoLayout 时代,必须有一个更简洁的解决方案。此外,您永远不想在工作时将这种东西发送给客户。
为了解决这个问题,而不是只调用referenceSizeForHeaderInSection:和返回CGSizeZero我创建的另一个子UICollectionReusableView用XIB1并设置里面的视图的高度0。
然后,我switch在viewForSupplementaryElementOfKind方法中包含的语句之外将该变体出列。这满足了界面生成器和视觉要求!
无论如何,在调试时在控制台中打印数百个不可满足的约束警告。
| 归档时间: |
|
| 查看次数: |
17195 次 |
| 最近记录: |