码迷,mamicode.com
首页 > 其他好文 > 详细

UICollectionView的简单使用

时间:2015-10-28 18:56:11      阅读:491      评论:0      收藏:0      [点我收藏+]

标签:

UICollectionViewCell

#import <UIKit/UIKit.h>

@interface JFCell : UICollectionViewCell

@property (nonatomic ,strong) UIImageView *cellImageView;

@property (nonatomic ,strong) UILabel *cellLabel;

@end
#import "JFCell.h"

@implementation JFCell
@synthesize cellImageView,cellLabel;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.cellImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-20)];
        self.cellImageView.backgroundColor = [UIColor brownColor];
        [self addSubview:self.cellImageView];
        
        self.cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height-20, frame.size.width, 20)];
        self.cellLabel.backgroundColor = [UIColor lightGrayColor];
        self.cellLabel.font = [UIFont systemFontOfSize:14];
        self.cellLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.cellLabel];
    }
    return self;
}
@end

UICollectionView

#import "ViewController.h"
#import "JFCell.h"
//每行的cell个数
#define counts 3
//边距
#define margin 5

static NSString *cellString = @"cell";

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *JFCollectionView;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //滑动方向(纵向)
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    JFCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    JFCollectionView.backgroundColor = [UIColor whiteColor];
    JFCollectionView.dataSource = self;
    JFCollectionView.delegate = self;
    //注册cell,重用
    [JFCollectionView registerClass:[JFCell class] forCellWithReuseIdentifier:cellString];
    [self.view addSubview:JFCollectionView];
}

#pragma mark ----------------------UICollectionViewDataSource-------------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 48;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JFCell *cell = [JFCollectionView dequeueReusableCellWithReuseIdentifier:cellString forIndexPath:indexPath];
    cell.cellLabel.text = [NSString stringWithFormat:@"我是第%li个Cell",(long)indexPath.row+1];
    return cell;
}

#pragma mark ----------------------UICollectionViewDelegateFlowLayout--------------
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat cellWidth = (self.view.frame.size.width-2*counts*margin)/counts;
    return CGSizeMake(cellWidth, cellWidth+20);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(margin, margin, margin, margin);
}

 技术分享

UICollectionView的简单使用

标签:

原文地址:http://www.cnblogs.com/fanzhiying/p/4918035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!