self.view.backgroundColor = [UIColor whiteColor];
// UICollectionViewLayout 不能直接使用,给collectionView的cell提前布局 prepareLayout(重写item的方法)
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//cell的大小item的大小
flowLayout.itemSize = CGSizeMake(155, 200);
//横向滚动
// flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//UICollectionView的基本使用
//参数2:布局文件
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
//collectionView也有两个代理
collectionView.delegate = self;
collectionView.dataSource = self;
//背景颜色
collectionView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:collectionView];
[collectionView release];
//collectionView必须要提前注册cell类
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"aaaa"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//cell的个数 Items;
return 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"aaaa" forIndexPath:indexPath];
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"aaa" forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
return cell;
}本文出自 “小刘_Blog” 博客,请务必保留此出处http://liuyafang.blog.51cto.com/8837978/1556802
原文:http://liuyafang.blog.51cto.com/8837978/1556802