美文网首页
ios 纯代码自定义UICollectionViewCell

ios 纯代码自定义UICollectionViewCell

作者: 缘來諟夢 | 来源:发表于2020-10-30 14:46 被阅读0次

很多时候我们都需要自定义UICollectionViewCell,于是就使用纯代码简单的写了一下UICollectionViewCell的自定义,

希望可以跟大家带来帮助

代码如下:

UICollectionViewCell.h 

@interface GKCollectionViewCell : UICollectionViewCell
@property(nonatomic ,strong)UIImageView *imgView;
@property(nonatomic ,strong)UILabel *text;
@property(nonatomic ,strong)UIButton *btn;
@end
UICollectionViewCell.m
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor purpleColor];
        self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, CGRectGetWidth(self.frame)-10, CGRectGetWidth(self.frame)-10)];
        self.imgView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:self.imgView];
        self.text = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(self.imgView.frame), CGRectGetWidth(self.frame)-10, 20)];
        self.text.backgroundColor = [UIColor brownColor];
        self.text.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.text];        
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.frame = CGRectMake(5, CGRectGetMaxY(self.text.frame), CGRectGetWidth(self.frame)-10,30);
        [self.btn setTitle:@"按钮" forState:UIControlStateNormal];
        self.btn.backgroundColor = [UIColor orangeColor];
        [self addSubview:self.btn];
    }
    return self;
}
ViewController.h
@interface ViewController : UIViewController{
    NSMutableArray *_cellArray;     //collectionView数据
} 
@property (nonatomic, strong) UICollectionView *collectionView;
@end
ViewController.m
#import "ViewController.h"
#import "GKCollectionViewCell.h"
#define fDeviceWidth ([UIScreen mainScreen].bounds.size.width)
#define fDeviceHeight ([UIScreen mainScreen].bounds.size.height)

@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:20.0f],NSFontAttributeName, nil]];
    self.navigationItem.title = @"自定义collectionView";
    /**
     *  创建collectionView self自动调用setter getter方法
     */
    [self.view addSubview:self.collectionView];
    /**
     *  加载的数据
     */
    NSArray *imgArray = [NSArray arrayWithObjects:@"IMG_0325.JPG",@"IMG_0325.JPG",@"IMG_0325.JPG",@"IMG_0325.JPG",@"IMG_0325.JPG",@"IMG_0325.JPG", nil];
    //collectionView数据
    _cellArray = [imgArray mutableCopy];
}
#pragma mark - 创建collectionView并设置代理
- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, fDeviceWidth, fDeviceHeight) collectionViewLayout:flowLayout];
        //定义每个UICollectionView 的大小

        flowLayout.itemSize = CGSizeMake((fDeviceWidth-20)/2, (fDeviceWidth-20)/2+50);

        //定义每个UICollectionView 横向的间距

        flowLayout.minimumLineSpacing = 5;

        //定义每个UICollectionView 纵向的间距

        flowLayout.minimumInteritemSpacing = 0;

        //定义每个UICollectionView 的边距距

        flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 5, 5);//上左下右

        

        //注册cell和ReusableView(相当于头部)

        [_collectionView registerClass:[GKCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        //设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //背景颜色
        _collectionView.backgroundColor = [UIColor whiteColor];
        //自适应大小
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    return _collectionView;
}
#pragma mark - UICollectionView delegate dataSource

#pragma mark 定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_cellArray count];
}
#pragma mark 定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
} 
#pragma mark 每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    GKCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    [cell sizeToFit];
        cell.imgView.image = [UIImage imageNamed:_cellArray[indexPath.item]];
    cell.text.text = [NSString stringWithFormat:@"Cell %ld",indexPath.item];
    //按钮事件就不实现了……
    return cell;
}
#pragma mark UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"选择%ld",indexPath.item);
}

相关文章

网友评论

      本文标题:ios 纯代码自定义UICollectionViewCell

      本文链接:https://www.haomeiwen.com/subject/luddvktx.html