UICollectionView總結
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]; }</pre>
兩個delegate和tableView的類型,返回datasouce和處理響應的事件!
用到UICollectionView的一個小項目Rando地址:http://git.oschina.net/jonear/Rando