Objective-C 自定义UICollectionView

本文介绍如何自定义UICollectionView,包括继承UICollectionView、重写初始化方法及数据源代理方法等步骤,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UICollectionView是iOS开发常用UI控件,自定义UICollectionView流程方便开发使用。

1.实现自定义CollectionView首先继承CollectionView.

#import <UIKit/UIKit.h>

@interface MyOrderMultyPartsCollectionCell : UICollectionViewCell

@end
2.重写初始化方法,实现代理以及数据源方法

#import "MyOrderMultyPartsCollectionView.h"
#import "MyOrderMultyPartsCollectionCell.h"

@interface MyOrderMultyPartsCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end
NSString * const reuseID = @"MyOrderMultyPartsCollectionCell";
@implementation MyOrderMultyPartsCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    self=[super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseID];
        self.backgroundColor=[UIColor whiteColor];
        self.delegate=self;
        self.dataSource=self;
    }
    return self;
}
    
#pragma mark ---- UICollectionViewDataSource
    
//定义每个Cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGSize size = CGSizeMake(100,100);
    return size;
}
//两个cell之间的间距(同一行的cell的间距)
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    return 4;
//}
//这个是两行cell之间的间距(上下行cell的间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//定义每个Section的四边间距
//-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//return UIEdgeInsetsMake(10, 0, 10, 10);//分别为上、左、下、右
//}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
    
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        cell.layer.masksToBounds = YES;
        cell.backgroundColor = [UIColor purpleColor];

        return cell;
}
@end
3.使用的时候

UICollectionViewFlowLayout* khlayout = [[UICollectionViewFlowLayout alloc] init];
//collection滚动方向
khlayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
//khlayout.itemSize=CGSizeMake(100, 100);
MyOrderMultyPartsCollectionView * cview = [[MyOrderMultyPartsCollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, contentView.height) collectionViewLayout:khlayout];[contentView addSubview:cview];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值