一.在VC里添加CollectionView,在CollectionView中cell直接添加控件,設(shè)置約束
二.設(shè)置好相關(guān)代理
三.抄寫(xiě)代碼
3.1 遵守協(xié)議
<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
3.2 自定義cell(不需要xib)
EditPhotoCell.h
EditPhotoCell.m
3.3.實(shí)現(xiàn)代理方法
[self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([EditPhotoCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([EditPhotoCell class])];
記住這里不需要registNib方法來(lái)注冊(cè)cell,因?yàn)閏ell是直接添加到Collection上了,否則會(huì)報(bào)錯(cuò)
- (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;
}