和UITableView类似,UICollectionView也有两个代理方法
但是多了layout布局 section和item,初始化一个collectionview必须要给定一个layout布局,并且必须在创建的时候就注册要使用的cell;
代码如下:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
/*
UICollectionView 和tableView一样有两个协议 多了layout布局 cell:1.section 2. item
*/
MainViewController *mainVC = [[MainViewController alloc]init];
self.window.rootViewController = mainVC;
[mainVC release];
return YES;
}
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end
#import "MainViewController.h"
#import "MyCollectionViewCell.h"
@interface MainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//UICollectionView 的使用
// 1. 初始化一个collectionView必须要给定一个layout布局对象
// UICollectionViewLayout 是一个抽象类 如果要利用的话 必须创建子类 不能直接使用
//系统写好的子类 UICollectionViewFlowLayout 是苹果写好的一个布局类,用于标准的瀑布流布局
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//每个cell大小
flowLayout.itemSize = CGSizeMake(100, 180);
//设置一行的话 就用高度控制 高度和设置的液面高度差不多就行
flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 10, 20);
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
//左右滚动
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//设置间隔距离 最小行间距 不能低于这个数
flowLayout.minimumLineSpacing = 1;
//每一行间距不低于设置的数 每一行中两个cell之间的最小距离
// flowLayout.minimumInteritemSpacing = 5;
collectionView.dataSource = self;
collectionView.delegate = self;
[self.view addSubview:collectionView];
[collectionView release];
[flowLayout release];
//2. collectionview 必须在创建的时候就注册要使用的cell
[collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}
- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
//3.
cell.contentView.backgroundColor = [UIColor redColor];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld",indexPath.item);
}
#import <UIKit/UIKit.h>
@interface MyCollectionViewCell : UICollectionViewCell
@property (nonatomic,retain)UILabel *myLabel;
@end
#import "MyCollectionViewCell.h"
@implementation MyCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.myLabel = [[UILabel alloc]init];
_myLabel.backgroundColor = [UIColor orangeColor];
_myLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:_myLabel];
[_myLabel release];
//布局最好不要再layout subview里
//如果换布局就用另一个类
}
return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
//在这里面给collectionviewcell布局子视图
_myLabel.frame = CGRectMake(5, 5, layoutAttributes.size.width - 10, layoutAttributes.size.height-10);
}
- (void)dealloc
{
[_myLabel release];
[super dealloc];
}
//fanye
//可用scrollView的属性
@end