我尝试为 collectionView 的每个部分添加一个标题。
没有标题的 collectionView 工作正常。当我Accessories Section Header
在 Interfacebilder 中选中标记时,标题将显示在模拟器中。
我添加了一个UICollectionViewFlowLayout
类(作为文件)并在viewDidLoad
我的UICollectionView
控制器中调用它。在 FlowLayout 中,我想配置 zIndex 和 Size。
现在标题不会显示在模拟器中,另外我在其中添加了断点,UICollectionViewFlowLayout
但它们永远不会被击中......
如何获取集合视图中显示的标题,以及如何以正确的方式影响标题的zIndex
和?size
SurroundViewController.h
#import <UIKit/UIKit.h>
@interface SurroundViewController : UICollectionViewController
@end
环绕视图控制器.m
#import "SurroundViewController.h"
#import "MagazineCell.h"
#import "LazyJoeHeaderLayout.h"
static NSString * const cellID = @"cellID";
@interface SurroundViewController ()
@end
@implementation SurroundViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.collectionView.collectionViewLayout = [[LazyJoeHeaderLayout alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MagazineCell *mCell = (MagazineCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
mCell.backgroundColor = [UIColor lightGrayColor];
return mCell;
}
#pragma mark Collection view Layout things
// Layout set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Settings Size for Item at index %d", indexPath.row);
CGSize mElementSize = CGSizeMake(104, 104);
return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}
// Layout: Set Edgees of whole screen - not mElementsize
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
@end
LazyJoeHeaderLayout.h
#import <UIKit/UIKit.h>
@interface LazyJoeHeaderLayout : UICollectionViewFlowLayout
@end
LazyJoeHeaderLayout.m
#import "LazyJoeHeaderLayout.h"
@implementation LazyJoeHeaderLayout
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
attributes.frame = CGRectMake(0, 0, 100, 100);
attributes.zIndex = 1024;
return attributes;
}
@end
MagazineCell.h 和 MagazineCell.m 未列为此问题不需要的。