将 UICollectionView 的一些常用的方法和属性搞了一下
UICollectionView 和 UITableView 用法差不多也是继承UIScrollView 的
1.常用的方法:
// 根据尺寸和布局方法来创建一个 UICollectionView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;
// 根据代码注册 UICollectionViewCell的类型
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
// 注册 footerView 和 HeaderView 的
- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
// 从缓冲池中取出 Cell 和头部尾部 View
- (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath;
- (id)dequeueReusableSupplementaryViewOfKind:(NSString*)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath;
// 设置 UICollectionView 的布局方式
- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated;
2.属性
// cell 是否能被点击
@property (nonatomic) BOOL allowsSelection; // default is YES
// cell 是否支持多点
@property (nonatomic) BOOL allowsMultipleSelection; // default is NO
3.UICollectionViewDataSource 数据源
// 多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
// 每组多少个 cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
// 返回 cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
// 返回 头部和尾部的 view
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
4.UICollectionViewDelegate代理方法
// 选中单元格 cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
UICollectionViewFlowLayout瀑布流布局
1.属性
scrollDirection // 滚动方向
minimumInteritemSpacing // 水平最小间距
minimumLineSpacing // 垂直最小间距
2.UICollectionViewDelegateFlowLayout继承 UICollectionViewDelegate代理方法
// 单元格的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
}
//cell与cell之间的间隔,边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
}
//设置标题头大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
}
//设置标题尾大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
}