创建UICollectionView的步骤:
与UITableView的区别:
1.使用系统的布局UICollectionViewFlowLayout
2.自定义Cell
// 苹果专门写了一个标准瀑布流结构的布局
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
// layout的使用
// 1.设置每个cell的大小
flowLayout.itemSize = CGSizeMake(50, 50);
// 2.设置collectionView的滚动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// 3.设置item间隔大小……
flowLayout.minimumInteritemSpacing = 20;
flowLayout.minimumLineSpacing = 20;
flowLayout.sectionInset = UIEdgeInsetsMake(30, 30, 30, 30);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
[collectionView setDelegate:self];
[collectionView setDataSource:self];
[collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"lalala"];
[self.view addSubview:collectionView];
[collectionView release];
实现collectionView的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.array count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"lalala" forIndexPath:indexPath];
[cell.contentView setBackgroundColor:[UIColor yellowColor]];
[cell.myLabel setText:[self.array objectAtIndex:indexPath.item]];
return cell;
}
与UITableView的区别:
1.必须提前注册一个cell
[collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"lalala"];
2.UICollectionViewCell内部没有任何label或者imageView,需要自定一个Cell
3.用一个类专门给collectionView提供布局信息