陈述一下简单流程:
1.首先定义:UICollectionViewFlowLayout
2.初始化UICollectionView
3.注册复用的cell,定义她们的reuseIndefinite
4.注册UICollectionView的delegate和DataSource代理
5.完成代理,ok
代码:
- (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = I_P_SP; layout.minimumInteritemSpacing = I_P_I_SP; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(, , self.width, ) collectionViewLayout:layout]; _collectionView.backgroundColor = def_color_white; [_collectionView registerClass:[IssuePhotosViewCell class] forCellWithReuseIdentifier:kPhotoCellIdentifier]; [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kAddCellIdentifier]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.showsVerticalScrollIndicator = NO; [self addSubview:_collectionView]; } return _collectionView;}
UICollectionView的Lazy Load
#pragma mark - UICollectionViewDelegate / DataSource / FlowLayout- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ , );}- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ // TODO 图片浏览器 NSLog(@"%ld",(long)[indexPath row]);}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ ;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == [self.assetsArray count]) { UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kAddCellIdentifier forIndexPath:indexPath]; [cell.contentView addSubview:self.addButton]; return cell; } else { IssuePhotosViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPhotoCellIdentifier forIndexPath:indexPath]; cell.asset = [self.assetsArray objectAtIndex:indexPath.row]; cell.indexPath = indexPath; cell.deletePhotoButton.tag = indexPath.row; [cell.deletePhotoButton addTarget:self action:@selector(deleteView:) forControlEvents:UIControlEventTouchUpInside]; return cell; }
UICollectionView的代理实现
复用问题,只要把界面和数据分开处理即可,界面初始化写在自定cell或者系统cell的初始化中,数据则每次重新更新即可