关于UICollectionView一些笔记

来源:互联网 发布:mac cad怎么样卸载软件 编辑:程序博客网 时间:2024/05/19 16:21


UICollectionView

http://code4app.com/forum.php?mod=viewthread&tid=9339&highlight=collectionview

一.什么是UICollectionView 

         它和UItableView类似,可以做九宫格布局的一种view。

 

二.如何创建UICollectionView

1.遵守协议

UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout

 

2.创建数据源

3.创建UI

       1)设置布局

UICollectionViewFlowLayout*layout = [[UICollectionViewFlowLayout alloc] init];

    //设置cell之间的上下边距

   layout.minimumLineSpacing = 10;

   //设置cell之间的左右边距

   layout.minimumInteritemSpacing = 10;

   //设置collectionView滚动方向

   layout.scrollDirection = UICollectionViewScrollDirectionVertical;

   

   //设置cell的大小 itemSize代表cell的大小

//   layout.itemSize = CGSizeMake(110, 150);

       2)将layout传入UICollectionView中实现初始化

_collectonView= [[UICollectionView alloc] initWithFrame:self.view.boundscollectionViewLayout:layout];

    _collectonView.backgroundColor = [UIColorwhiteColor];

    //外观

   _collectonView. delegate = self;

   //数据源

   _collectonView.dataSource = self;

  _collectonView.showsVerticalScrollIndicator = NO;

 

#pragmamark --- UICollectionViewDelegate

 

//设置段个数

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView

{

   return self.dataArrM.count;

}

 

//设置每一段cell个数

-(NSInteger)collectionView:(UICollectionView *)collectionViewnumberOfItemsInSection:(NSInteger)section

{

   Group *group = self.dataArrM[section];

   return group.photoArrM.count;

}

 

//设置cell

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath

{

   CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];

   

   Group *group = self.dataArrM[indexPath.section];

   Photo *p1 = group.photoArrM[indexPath.row];

   

   cell.imgView.image = [UIImage imageNamed:p1.photoName];

   cell.nameLabel.text = p1.name;

   

   return cell;

}

 

//设置cell的偏移量

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionViewlayout:(UICollectionViewLayout *)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section

{

   //上 左 下 右 控制cell的位置

   return UIEdgeInsetsMake(10, 12, 10, 12);

}

 

//设置cell的大小

-(CGSize)collectionView:(UICollectionView *)collectionViewlayout:(UICollectionViewLayout *)collectionViewLayoutsizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

   return CGSizeMake(200, 150);

}

 

//设置段头高度

-(CGSize)collectionView:(UICollectionView *)collectionViewlayout:(UICollectionViewLayout *)collectionViewLayoutreferenceSizeForHeaderInSection:(NSInteger)section

{

   return CGSizeMake(375, 25);

}

 

//设置段头

- (UICollectionReusableView *)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString *)kindatIndexPath:(NSIndexPath *)indexPath

{

   if ([kind isEqualToString:UICollectionElementKindSectionHeader])

   {

       UICollectionReusableView *headView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"head" forIndexPath:indexPath];

       Group *group = self.dataArrM[indexPath.section];

       UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375,25)];

       label.textAlignment  =NSTextAlignmentCenter;

       label.text = group.groupName;

       label.backgroundColor = [UIColor orangeColor];

       label.tag = 100 + indexPath.section;

       [headView addSubview:label];

       

       UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(delete:)];

       label.userInteractionEnabled = YES;

       [label addGestureRecognizer:tap];

       

       return headView;

   }else

    {

   }

   

   return nil;

}

 

- (void)delete:(UITapGestureRecognizer*)tap

{

   [self.dataArrM removeObjectAtIndex:tap.view.tag - 100];

   

   [_collectonView reloadData];

}

 

//点击cell触发

- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

   Group *group = self.dataArrM[indexPath.section];

   Photo *p1 = group.photoArrM[indexPath.row];

   

   //传图片名字

   NSLog(@"点击了%@",p1.name);

}

 

//是否允许点击cell

- (BOOL)collectionView:(UICollectionView*)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

   if (indexPath.section == 0) {

       if (indexPath.row == 1) {

            return NO;

       }

   }

   return YES;

}

 

注意:Cell和段头和段尾都要注册

要调用要先注册

- (void)viewDidLoad {

//注册手写cell

   [_collectonView registerClass:[CustomCell class]forCellWithReuseIdentifier:@"cell"];

   

   //手写注册cell段头

   [_collectonView registerClass:[UICollectionReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];

   

   //手写注册cell段尾

    [_collectonViewregisterClass:[UICollectionReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"footer"];

 

}

 

 

 

 

0 0
原创粉丝点击