UICollectionView总结

dy223 9年前

1.基本介绍

先介绍一下UICollectionView,大家应该都用过UITableView,不熟悉的可以看这里《UITableView总结》,UITableView中的表格只支持单排列表,没办法支持网格列表模式

当然也有很多大牛使用UITableView做出网格效果来了,实现的方式肯定都一样,就是将Cell分成几部分View,在赋值的时候一次性传两个或者多个data过去,通过delegate或者其他方式返回不同cell被点击的效果,要求具体Demo嘛,我找找看啊!

当然拉,上面的方法可行,但是麻烦,而且不好维护,在IOS6 SDK中就出了UICollectionView(只支持ios6以上系统),首先UICollectionView是基础UITableView的,所以UICollectionView的结构模式是和UITableView一模一样的。

2.使用方法

UICollectionView的使用方法和UITableView的基本相似,不同的是UICollectionViewFlowLayout和UICollectionViewCell

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];      [flowLayout setItemSize:CGSizeMake(CAPTURE_SIZE/2, CAPTURE_SIZE/2)];      [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];      flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);      flowLayout.minimumLineSpacing = 10;      flowLayout.minimumInteritemSpacing = 0;      flowLayout.footerReferenceSize = CGSizeMake(300, 30);            _collectionView.delegate = self;      _collectionView.dataSource = self;      [_collectionView setCollectionViewLayout:flowLayout];      [_collectionView registerClass:[RDImgeCollectionCell class] forCellWithReuseIdentifier:@"RDImgeCollectionCell"];      [_collectionView setBackgroundColor:[UIColor whiteColor]];

UICollectionViewFlowLayout决定了UICollectionViewCell将要显示的大小,间隔,排列方式等

#pragma -mark CollectionView DataSource    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{      return _myRandoImageArray.count;  }    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{            static NSString *identifier = @"RDImgeCollectionCell";            RDImgeCollectionCell *cell = (RDImgeCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];            RDImageModel *image = [_myRandoImageArray objectAtIndex:indexPath.row];      [cell.imageView setImageWithURL:[NSURL URLWithString:image.imageUrl] placeholderImage:[UIImage imageNamed:@"defaultImage"]];      [cell showLikeMsg:image.likeNumber];            return cell;  }    #pragma -mark CollectionView UICollectionViewDelegate  - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  {      RDImageModel *image = [_myRandoImageArray objectAtIndex:indexPath.row];      RDDetailImageViewController *detailImageViewController = [[RDDetailImageViewController alloc] init];      detailImageViewController.imageArray = _myRandoImageArray;      detailImageViewController.thisImage = image;      detailImageViewController.isMyImage = YES;      [_navController pushViewController:detailImageViewController animated:YES];  }

两个delegate和tableView的类型,返回datasouce和处理响应的事件!

用到UICollectionView的一个小项目Rando地址:http://git.oschina.net/jonear/Rando