很久没写博客了,最近闲,多写点!ios
先介绍一下UICollectionView,你们应该都用过UITableView,不熟悉的能够看这里《UITableView总结》,UITableView中的表格只支持单排列表,没办法支持网格列表模式git
固然也有不少大牛使用UITableView作出网格效果来了,实现的方式确定都同样,就是将Cell分红几部分View,在赋值的时候一次性传两个或者多个data过去,经过delegate或者其余方式返回不一样cell被点击的效果,要求具体Demo嘛,我找找看啊!ide
固然拉,上面的方法可行,可是麻烦,并且很差维护,在IOS6 SDK中就出了UICollectionView(只支持ios6以上系统),首先UICollectionView是基础UITableView的,因此UICollectionView的结构模式是和UITableView如出一辙的。this
UICollectionView的使用方法和UITableView的基本类似,不一样的是UICollectionViewFlowLayout和UICollectionViewCellspa
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将要显示的大小,间隔,排列方式等.net
#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和处理响应的事件!code
用到UICollectionView的一个小项目Rando地址:http://git.oschina.net/jonear/Randoblog