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

无限滚动 UICollectionView

时间:2015-04-18 23:25:12      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

注意拖控件的时候 一定注意代理的实现

 

 

 

//

//  ViewController.m

//  ImageView

//

//  Created by Lenny on 4/18/15.

//  Copyright (c) 2015 Lenny Kwok. All rights reserved.

//

 

#import "ViewController.h"

#import "LKImageCell.h"

 

#define Identifier @"collectionCell"

 

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    //进行cell的注册

     // 注册cell(如果缓存池中没有HMCellIdentifier对应的cell,就会自动创建HMCellIdentifier对应的注册过的cell)

    [self.collectionView registerClass:[LKImageCell class] forCellWithReuseIdentifier:Identifier];

}

 

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

    return  16;

}

 

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

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

    LKImageCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:Identifier forIndexPath:indexPath];

    cell.icon = [NSString stringWithFormat:@"minion_%02ld",indexPath.item + 1];

    return cell;

}

 

@end

 

 

 

 

 

//

//  LKImageCell.m

//  ImageView

//

//  Created by Lenny on 4/18/15.

//  Copyright (c) 2015 Lenny Kwok. All rights reserved.

//

 

#import "LKImageCell.h"

 

 

@interface LKImageCell ()

@property(nonatomic,weak) UIImageView * imageView;

 

@end

@implementation LKImageCell

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        UIImageView * imageView = [[UIImageView alloc]init];

        [self addSubview:imageView];

        self.imageView = imageView;

    }

    return self;

}

-(void)setIcon:(NSString *)icon

{

    _icon = [icon copy];

    self.imageView.image = [UIImage imageNamed:icon];

}

-(void)layoutSubviews

{

    [super layoutSubviews];

    CGFloat x = 5;

    CGFloat y = 0;

    CGFloat w = self.bounds.size.width - 2 * x;

    CGFloat h = self.bounds.size.height;

    self.imageView.frame = CGRectMake(x, y, w, h);

}

@end

 

 

 

 

//

//  LKImageCell.h

//  ImageView

//

//  Created by Lenny on 4/18/15.

//  Copyright (c) 2015 Lenny Kwok. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface LKImageCell : UICollectionViewCell

@property(nonatomic , copy) NSString * icon;

 

@end



无限滚动 UICollectionView

标签:

原文地址:http://www.cnblogs.com/LennyKwok/p/4438193.html

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