UICollectionView使用步骤
1.创建布局类UICollectionViewFlowLayout
2.创建UICollectionView、UICollectionViewCell的子类并注册
3.实现UICollectionView的代理方法
初始化UICollectionView、UICollectionViewFlowLayout
-(void)initCollectionView{
//1.创建布局流
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//2.初始化UICollectionView
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
self.collectionView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
//3.注册 cell、header、footer
[self.collectionView registerClass:[EFAccessControlcell class] forCellWithReuseIdentifier:@"cell"];
[self.collectionView registerClass:[CollectionHeaderView Class] forSupplementaryViewOfKind:@"CollectionHeaderView" withReuseIdentifier:UICollectionElementKindSectionHeader ];
[self.collectionView registerClass:[CollectionFooterView Class] forSupplementaryViewOfKind:@"CollectionFooterView" withReuseIdentifier:UICollectionElementKindSectionFooter ];
[self.view addSubview:self.collectionView];
}
创建cell
@interface EFAccessControlcell : UICollectionViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *itemImageView;
@end
@implementation EFAccessControlcell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.cornerRadius = 6;
_titleLabel = [[UILabel alloc] init];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.titleLabel];
_itemImageView = [[UIImageView alloc] init];
[self.contentView addSubview:self.itemImageView];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.itemImageView.bounds = CGRectMake(0, 0, 50, 50);
self.itemImageView.center = CGPointMake(self.bounds.size.width/2, 30);
self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.itemImageView.frame) + 5, self.bounds.size.width, 30);
}
@end
代理方法
-(UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
EFAccessControlcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
EFAccessPermissionModel *model = self.dataSource[indexPath.row];
cell.itemImageView.image = [UIImage imageNamed:model.iconName];
cell.titleLabel.text = model.titleName;
return cell;
}
//点击cell响应事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
}
//返回section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 10;
}
//返回cell的个数
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataSource.count;
}
#pragma mark - collectionViewLayout
//cell大小 cell的宽度决定了一行有几个cell
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.view.frame.size.width/2 - 20, 100);
}
//行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
//设置cell的外边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 10, 0, 10);
}
//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(self.view.bounds.size.width, 40);
}
//footer的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
return CGSizeMake(self.view.bounds.size.width, 24);
}
//
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *supplementaryView = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
CollectionHeaderView* header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView" forIndexPath:indexPath];
supplementaryView = header;
} else {
CollectionFooterView* footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionFooterView" forIndexPath:indexPath];
supplementaryView = footer;
}
return supplementaryView;
}
#pragma mark - collectionViewCell点击高亮
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor lightGrayColor];
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}