0

我尝试为 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 未列为此问题不需要的。

4

1 回答 1

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 
于 2013-11-11T12:58:03.813 回答