一.在VC里添加CollectionView,在CollectionView中cell直接添加控件,設置約束
二.設置好相關代理
三.抄寫代碼
3.1 遵守協議
<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
3.2 自定義cell(不需要xib)
EditPhotoCell.h
EditPhotoCell.m
3.3.實現代理方法
[self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([EditPhotoCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([EditPhotoCell class])];
記住這里不需要registNib方法來注冊cell,因為cell是直接添加到Collection上了,否則會報錯
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section{
? ? return 10;
}
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath{
? ? return CGSizeMake(70, 100);
}
- (nonnull__kindofUICollectionViewCell*)collectionView:(nonnullUICollectionView*)collectionView cellForItemAtIndexPath:(nonnullNSIndexPath*)indexPath {
? ? EditPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([EditPhotoCell class]) forIndexPath:indexPath];
//? ? cell.model = self.dataSource[indexPath.item];
? ? returncell;
}