我尝试在collectionView的每个部分添加一个标题。
没有标题的collectionView工作正常。当我在Interfacebilder中勾选Accessories Section Header
时,标题将显示在模拟器中。
我添加了UICollectionViewFlowLayout
类(作为文件),并在viewDidLoad
控制器的UICollectionView
内调用它。在FlowLayout中,我想配置zIndex和Size。
现在标题将不会显示在模拟器中,另外我在UICollectionViewFlowLayout
中添加了断点但它们永远不会被击中......
如何获取集合视图中显示的标题,以及如何以正确的方式影响标题的zIndex
和size
?
#import <UIKit/UIKit.h>
@interface SurroundViewController : UICollectionViewController
@end
#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
#import <UIKit/UIKit.h>
@interface LazyJoeHeaderLayout : UICollectionViewFlowLayout
@end
#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未列出此问题不需要。
答案 0 :(得分:0)
找到一个解决方案,在我的问题的更新代码下面显示标题大小的标题。
#import "SurroundViewController.h"
#import "MagazineCell.h"
#import "LazyJoe.h"
static NSString * const cellID = @"cellID";
@interface SurroundViewController ()
@property (nonatomic, strong) LazyJoe *lazyJoe;
@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.lazyJoe = [[LazyJoe alloc]init];
self.collectionView.collectionViewLayout = self.lazyJoe; // Layout things are in the Flowlayout
}
- (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;
}
#import "LazyJoe.h"
@implementation LazyJoe
- (id)init {
if (!(self = [super init])) return nil;
self.itemSize = CGSizeMake(104, 104);
self.sectionInset = UIEdgeInsetsMake(0,0,0,0); // That the items can align as nearest as possible
self.minimumInteritemSpacing = 2.0f; // spacing between items in one row
self.minimumLineSpacing = 2.0f; // spacing between lines
self.headerReferenceSize = CGSizeMake(self.collectionView.frame.size.width, 140);
return self;
}
@end