您的位置:首页 > 产品设计 > UI/UE

UICollectionViewController简单操作

2015-06-24 13:46 441 查看
//
//  MyCollectionViewCell.h
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/24.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyCollectionViewCell : UICollectionViewCell

@property(nonatomic,strong)UILabel *label;

@end
//
//  MyCollectionViewCell.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/24.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "MyCollectionViewCell.h"

@implementation MyCollectionViewCell

@synthesize label;

- (instancetype)initWithFrame:(CGRect)frameA{
self = [super initWithFrame:frameA];
if (self) {
self.backgroundColor = [UIColor orangeColor];
label = [[UILabel alloc] init];
label.frame = CGRectMake(25, 35, 50, 30);
label.backgroundColor = [UIColor purpleColor];
[self addSubview:label];
}
return self;

}

@end

//
//  MyCollectionViewController.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/24.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "MyCollectionViewCell.h"

@interface MyCollectionViewController ()

@end

@implementation MyCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"集合视图";
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;

//  注册这个
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

}

#pragma mark <UICollectionViewDataSource>

// 分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}

// 一个区有多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.label.text = [NSString stringWithFormat:@"%ld",indexPath.row];
cell.label.textColor = [UIColor redColor];
cell.label.textAlignment = NSTextAlignmentCenter;

return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

}
*/

@end

// 集合视图
// 创建流逝布局
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
// 单元格大小
flow.itemSize = CGSizeMake(100, 100);
// 滚动的方向
flow.scrollDirection = UICollectionViewScrollDirectionVertical;
// 设置边界
flow.sectionInset = UIEdgeInsetsMake(40, 0, 0, 0);

MyCollectionViewController *collectionDemo = [[MyCollectionViewController alloc] initWithCollectionViewLayout:flow];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UICollectionViewCont