UICollectionView
1 创建一个layout
//先创建UICollectionViewFlowLayout的对象
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
//同一行相邻两个cell的最小间距
layout.minimumInteritemSpacing = 5;
//最小两行之间的间距
layout.minimumLineSpacing = 5;
在这里还可以设置大小和上左下右等。。
UICollectionView *view = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 375, 667) collectionViewLayout:layout];
view.backgroundColor=[UIColor whiteColor];
view.delegate=self;
view.dataSource=self;
//这个是横向滑动
//layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
[self.view addSubview:view];
2 必须注册cell
//这种是xib建的cell 需要这么注册
UINib *cellNib=[UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
[_collectionView registerNib:cellNib forCellWithReuseIdentifier:@"CollectionViewCell"];
//这是头部与脚部的注册
UINib *cellNib1=[UINib nibWithNibName:@"CollectionReusableView" bundle:nil];
[_collectionView registerNib:cellNib1 forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView"];
[_collectionView registerNib:cellNib1 forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionReusableView"];
//cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//cell被电击后移动的动画
[collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
}
//头部试图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(50, 60);
}
//头部和脚部的加载
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *view=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableView"forIndexPath:indexPath];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(110, 20, 100, 30)];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
label.text=@"头";
}else{
label.text=@"脚";
}
[view addSubview:label];
return view;
}
//脚部试图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(50, 60);
}