1、创建与基础设置
复制
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumLineSpacing = 0;
flowLayout.minimumInteritemSpacing = 0;
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
[self.view addSubview:collectionView];
collectionView.dataSource = self;
collectionView.delegate = self;
collectionView.frame = self.view.bounse;
collectionView.backgroundColor = [UIColor clearColor];
collectionView.pagingEnabled = YES;
collectionView.bounces = NO;
collectionView.showsHorizontalScrollIndicator = NO;
[collectionView registerClass:[CustomCollectionCell class] forCellWithReuseIdentifier:CustomCollectionCellID];
2、设置数据源与代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.myDataArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)
indexPath {
return CGSizeMake((self.view.bounds.size.width - 40) / 3, 150);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CustomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CustomCollectionCellID forIndexPath:indexPath];
CustomItem *dataItem = self.myDataArray[indexPath.row];
cell.dataItem = dataItem;
return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(20, 10, 20, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
}
3、自定义cell
-
3.1 CustomCollectionCell.h
@class CustomItem;
@interface CustomCollectionCell : UICollectionViewCell
@property(nonatomic, strong) CustomItem *dataItem;
@end
-
3.2 CustomCollectionCell.m
@interface CustomCollectionCell ()
@end
@implementation CustomCollectionCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
}
return self;
}
- (void)setDataItem:(CustomItem *)dataItem {
_dataItem = dataItem;
}
- (void)layoutSubviews {
[super layoutSubviews];
}
@end
4、自定义分段头尾的创建与引用
-
4.1 引用
[collectionView registerClass:[myHeaderFooterView1 class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
[collectionView registerClass:[myHeaderFooterView1 class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)
section {
return CGSizeMake(20, 30);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)
section {
return CGSizeMake(20, 30);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)
indexPath {
myHeaderFooterView1 *view = nil;
if (kind == UICollectionElementKindSectionHeader) {
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
view.nameLabel.text = [NSString stringWithFormat:@"第 %ld 段 Header", indexPath.section];
}
else {
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
view.nameLabel.text = [NSString stringWithFormat:@"第 %ld 段结束 Footer", indexPath.section];
}
return view;
}
-
4.2 创建
@interface myHeaderFooterView : UICollectionReusableView
@property(nonatomic, retain)UILabel *nameLabel;
@end
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_nameLabel = [[UILabel alloc] initWithFrame:self.bounds];
_nameLabel.textAlignment = NSTextAlignmentCenter;
_nameLabel.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_nameLabel];
}
return self;
}
5、自定义布局风格
@interface CustomLayout : UICollectionViewLayout
@implementation CustomLayout
- (CGSize)collectionViewContentSize {
return CGSizeMake(self.collectionView.bounds.size.width, [self.collectionView numberOfItemsInSection:0 / 3] * 200 + 200);
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *attributesArray = [[NSMutableArray alloc] init];
NSUInteger cellCount = [self.collectionView numberOfItemsInSection:0];
for (int i = 0; i < cellCount; i++) {
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
[attributesArray addObject:attributes];
}
return attributesArray;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGFloat largeCellSide = 200;
CGFloat smallCellSide = 100;
NSUInteger lineSpacing = 5;
UIEdgeInsets insets = UIEdgeInsetsMake(2, 20, 2, 20);
NSInteger line = indexPath.item / 3;
CGFloat lineOriginY = insets.top + largeCellSide * line + lineSpacing * line;
CGFloat rightLargeX = self.collectionView.bounds.size.width - largeCellSide - insets.right;
CGFloat rightSmallX = self.collectionView.bounds.size.width - smallCellSide - insets.right;
if (indexPath.item % 6 == 0) {
attribute.frame = CGRectMake(insets.left, lineOriginY, largeCellSide, largeCellSide);
}
else if (indexPath.item % 6 == 1) {
attribute.frame = CGRectMake(rightSmallX, lineOriginY, smallCellSide, smallCellSide);
}
else if (indexPath.item % 6 == 2) {
attribute.frame = CGRectMake(rightSmallX, lineOriginY + smallCellSide + insets.top, smallCellSide, smallCellSide);
}
else if (indexPath.item % 6 == 3) {
attribute.frame = CGRectMake(insets.left, lineOriginY, smallCellSide, smallCellSide);
}
else if (indexPath.item % 6 == 4) {
attribute.frame = CGRectMake(insets.left, lineOriginY + smallCellSide + insets.top, smallCellSide, smallCellSide);
}
else if (indexPath.item % 6 == 5) {
attribute.frame = CGRectMake(rightLargeX, lineOriginY, largeCellSide, largeCellSide);
}
return attribute;
}
CustomLayout *layout = [[CustomLayout alloc] init];
UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height - 20) collectionViewLayout:layout];
6、获取Cell
- (void)collectionView:(UICollectionView *)collection_view didSelectItemAtIndexPath:(NSIndexPath *)index_path {
UICollectionViewCell *reuse_cell = [collection_view cellForItemAtIndexPath:index_path];
reuse_cell.backgroundColor = GC13_Color;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
G26_Edit_Controller *push_vc = [[G26_Edit_Controller alloc] init];
NSMutableDictionary *data_item = G26_Handle.sub_Array[index_path.row];
push_vc.data_Item = data_item;
[self push_Controller:push_vc];
reuse_cell.backgroundColor = GC12_Color;
});
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)