https://my.oschina.net/iq19900204/blog/391972

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

你可能感兴趣的:(https://my.oschina.net/iq19900204/blog/391972)