iOS UI 18 uicollectionview和自定义cell

来源:互联网 发布:mac os10.8.5升级10.9 编辑:程序博客网 时间:2024/05/16 10:24

//

//  RootViewController.m

//  Ui - 19  _ UICollectionView

//

//  Created by dllo on 15/12/3.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "RootViewController.h"

#import "CustomCollectionViewCell.h"

@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@end


@implementation RootViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

    

    //uicollectviewLayout 是一个抽象类,我们一般使用他的子类UICollectionViewFlowLayout

    UICollectionViewFlowLayout *flowl = [[[UICollectionViewFlowLayoutalloc]init]autorelease];

    //行间距

    //注意:1,系统会尽可能靠近此参数,并保证不会小于

    //垂直与滑动方向即为行!

    flowl.minimumLineSpacing = 50;

  

    //列间距

    flowl.minimumInteritemSpacing = 50;

    //单元大小

    flowl.itemSize = CGSizeMake(100, 100);

    //区头间据

    flowl.headerReferenceSize =CGSizeMake(50, 50);

    

    //区脚间距

    flowl.footerReferenceSize =CGSizeMake(100, 100);

    

    

    //与屏幕四周的间距

    flowl.sectionInset = UIEdgeInsetsMake(10, 30, 10, 30);

    //滚动方向

    flowl.scrollDirection =UICollectionViewScrollDirectionHorizontal;

    

    

    UICollectionView *collectionV = [[UICollectionViewalloc]initWithFrame:self.view.framecollectionViewLayout:flowl];

    collectionV.backgroundColor = [UIColorpurpleColor];

    collectionV.delegate = self;

    collectionV.dataSource = self;

    [self.viewaddSubview:collectionV];

    [collectionV release];

    

    //注册自定义cell

    [collectionV registerClass:[CustomCollectionViewCellclass] forCellWithReuseIdentifier:@"cell"];

    

    

    

    // Do any additional setup after loading the view.

}

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

{

    return 100;

}

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

{

    CustomCollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];

    cell.label1.text =[NSStringstringWithFormat:@"%ld",indexPath.row];

    return cell;

    

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 3;

}

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

{

    NSLog(@"点击%ld", indexPath.row);

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end

//

//  CustomCollectionViewCell.h

//  Ui - 19  _ UICollectionView

//

//  Created by dllo on 15/12/3.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface CustomCollectionViewCell : UICollectionViewCell

@property (nonatomic,retain)UILabel *label1;

@end


//

//  CustomCollectionViewCell.m

//  Ui - 19  _ UICollectionView

//

//  Created by dllo on 15/12/3.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "CustomCollectionViewCell.h"


@implementation CustomCollectionViewCell

- (void)dealloc

{

    [_label1 release];

    [super dealloc];

    

}

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        [self create];

    }

    return self;

}

- (void)create

{

    self.backgroundColor = [UIColorredColor];

    

    

    self.label1 = [[UILabelalloc]initWithFrame:CGRectMake(0,0 , 30, 30)];

    [self.contentViewaddSubview:self.label1];

    self.label1.backgroundColor = [UIColorwhiteColor];

    [self.label1release];

}

@end



0 0
原创粉丝点击