ios 自定义UICollectionViewCell

#import

@interface PublishCell : UICollectionViewCell

@property (strong, nonatomic) UIImageView *topImage;

@property (strong, nonatomic) UILabel *btmlabel;

#import "PublishCell.h"

@implementation PublishCell

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self)

    {

        self.topImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 70, 70)];

        self.topImage.layer.masksToBounds = YES;

        self.topImage.layer.cornerRadius = 20;

        self.topImage.backgroundColor = [UIColor redColor];

        [self.contentView addSubview:self.topImage];


        self.btmlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 70, 20)];

        self.btmlabel.textAlignment = NSTextAlignmentCenter;

        self.btmlabel.textColor = [UIColor blackColor];

        self.btmlabel.font = [UIFont fontWithName:@"Verdana-Bold"size:19];

        [self.contentView addSubview:self.btmlabel];

    }


    return self;

}

@end


#import

@interface PublishViewController : UIViewController

@end

#import "PublishViewController.h"

#import "PublishCell.h"

@interface PublishViewController () {

    UICollectionView *mainCollectionView;

}

@end

@implementation PublishViewController

- (void)viewDidLoad {

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    self.title = @"发布";

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];


    //1.初始化layout

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    //设置collectionView滚动方向

    //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

    //设置headerView的尺寸大小

    //layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);

    //该方法也可以设置itemSize

    layout.itemSize =CGSizeMake(90, 110);


    //2.初始化collectionView

    mainCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

    [self.view addSubview:mainCollectionView];

    mainCollectionView.backgroundColor = [UIColor clearColor];


    //3.注册collectionViewCell

    //注意,此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId

    [mainCollectionView registerClass:[PublishCell class] forCellWithReuseIdentifier:@"cellId"];


    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView

    [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];


    //4.设置代理

    mainCollectionView.delegate = self;

    mainCollectionView.dataSource = self;

}

#pragma mark collectionView代理方法

//返回section个数

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 1;

}

//每个section的item个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return 12;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


    NSString *tablePList = [[NSBundle mainBundle] pathForResource:@"tableList" ofType:@"plist"];

    NSArray *tableData = [[NSArray alloc] initWithContentsOfFile:tablePList];


    PublishCell *cell = (PublishCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];


    [cell.topImage setImage:[UIImage imageNamed:tableData[indexPath.row][@"leftImage"]]];

    cell.btmlabel.text = tableData[indexPath.row][@"titleLabel"];


    return cell;

}

//设置每个item的尺寸

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

//{

//    return CGSizeMake(90, 90);

//}

//footer的size

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

//{

//    return CGSizeMake(10, 10);

//}

//header的size

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

//{

//    return CGSizeMake(10, 10);

//}

//设置每个item的UIEdgeInsets

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    return UIEdgeInsetsMake(20, 20, 20, 20);

}

//设置每个item水平间距

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

//{

//    return 20;

//}

//设置每个item垂直间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{

    return 20;

}

//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致,本例都为 “reusableView”

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];

    headerView.backgroundColor =[UIColor grayColor];

    UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];

    label.text = @"这是collectionView的头部";

    label.font = [UIFont systemFontOfSize:20];

    [headerView addSubview:label];

    return headerView;

}

//点击item方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    PublishCell *cell = (PublishCell *)[collectionView cellForItemAtIndexPath:indexPath];

    NSString *msg = cell.btmlabel.text;

    NSLog(@"%@",msg);

}

- (UIStatusBarStyle)preferredStatusBarStyle {

    return UIStatusBarStyleLightContent;

}

@end

你可能感兴趣的:(ios 自定义UICollectionViewCell)