很久沒寫博客了,最近閒,多寫點!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