// ProfileViewController.m
// scxhgh
// Created by xmkjsoft on 2024/7/16.
#import "rofileViewController.h"
@interface ProfileViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView; // 声明UICollectionView属性
@end
@implementation ProfileViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置配景色
self.view.backgroundColor = [UIColor whiteColor];
// 创建并设置UICollectionView
[self setupCollectionView];
}
- (void)setupCollectionView {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(100, 100); // 设置每个cell的大小
layout.minimumLineSpacing = 10; // 设置行间距
layout.minimumInteritemSpacing = 10; // 设置列间距
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); // 设置section的内边距
// 设置header的大小
layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50);
// 设置footer的大小
layout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 50);
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
// 注册cell类
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier"cellIdentifier"];
// 注册header类
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier"headerIdentifier"];
// 注册footer类
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier"footerIdentifier"];
[self.view addSubview:self.collectionView];
}
- (UICollectionReusableView *)collectionViewUICollectionView *)collectionView viewForSupplementaryElementOfKindNSString *)kind atIndexPathNSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier"headerIdentifier" forIndexPath:indexPath];
// 移除之前的所有子视图,避免复用标题
[headerView.subviews makeObjectsPerformSelectorselector(removeFromSuperview)];
// 添加一个UILabel到headerView
UILabel *titleLabel = [[UILabel alloc] initWithFrame:headerView.bounds];
titleLabel.text = @"这是Header";
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor blackColor];
[headerView addSubview:titleLabel];
return headerView;
} else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier"footerIdentifier" forIndexPath:indexPath];
// 移除之前的所有子视图,避免复用标题
[footerView.subviews makeObjectsPerformSelectorselector(removeFromSuperview)];
// 添加一个UILabel到footerView
UILabel *footerLabel = [[UILabel alloc] initWithFrame:footerView.bounds];
footerLabel.text = @"这是Footer";
footerLabel.textAlignment = NSTextAlignmentCenter;
footerLabel.textColor = [UIColor grayColor];
[footerView addSubview:footerLabel];
return footerView;
}
return nil;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionViewUICollectionView *)collectionView {
return 1; // 返回section的数目
}
- (NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section {
return 5; // 返回每个section中item的数目
}
- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor systemBlueColor]; // 设置cell的配景色
// 可以在这里添加更多的自界说内容,好比在cell上添加一个UILabel
UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
label.text = [NSString stringWithFormat"%ld", (long)indexPath.row];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
[cell.contentView addSubview:label];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionViewUICollectionView *)collectionView layoutUICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// 设置列数
NSInteger numberOfColumns = 5;
// 获取当前屏幕的宽度
CGFloat totalWidth = collectionView.bounds.size.width;
// 设置section的内边距(与layout.sectionInset同等)
CGFloat sectionInset = 10.0;
// 设置item之间的最小列间距(与layout.minimumInteritemSpacing同等)
CGFloat minimumInteritemSpacing = 10.0;
// 盘算item的宽度
CGFloat itemWidth = (totalWidth - (sectionInset * 2) - ((numberOfColumns - 1) * minimumInteritemSpacing)) / numberOfColumns;
// 返回item的大小
return CGSizeMake(itemWidth, itemWidth);
}
@end
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |