您的位置:首页 > 产品设计 > UI/UE

IOS之UICollectionView初探(代码实现)

2014-02-27 14:11 821 查看
自定义一个类,用于展现UICollectionView中cell的展现,看下我上一篇写的文章,只不过把继承UIView改成UICollectionViewCell

我在我自己定义的一个VIew中实现UICollectionView的展现

首先.h文件为

//
// IKEDMyOwnHorizenView.h
// Ikefr
//
// Created by apple on 14-2-27.
// Copyright (c) 2014年 com.tyust. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface IKEDMyOwnHorizenView : UIView<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView *myCollectionView;

-(void)setImgData:(NSArray*)array;

@end


.m文件为

//
// IKEDMyOwnHorizenView.m
// Ikefr
//
// Created by apple on 14-2-27.
// Copyright (c) 2014年 com.tyust. All rights reserved.
//

#import "IKEDMyOwnHorizenView.h"
#import "IKEDMyOwnImgView.h"

@implementation IKEDMyOwnHorizenView
{
NSArray *collectionImageData;

}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.itemSize = CGSizeMake(100, 100);
flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
flowLayout.minimumInteritemSpacing = 10;

_myCollectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];

_myCollectionView.dataSource = self;
_myCollectionView.delegate = self;
_myCollectionView.showsHorizontalScrollIndicator = NO;

self.backgroundColor = [UIColor whiteColor];

[_myCollectionView registerClass:[IKEDMyOwnImgView class] forCellWithReuseIdentifier:@"IKeD"];

[self addSubview:_myCollectionView];

}
return self;
}

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

return collectionImageData.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

IKEDMyOwnImgView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IKeD" forIndexPath:indexPath];

NSDictionary *dic = [collectionImageData objectAtIndex:[indexPath row]];

[cell setImg:[UIImage imageNamed:[dic objectForKey:@"pic"]]];
[cell setTitle:[dic objectForKey:@"title"]];

return cell;
}

-(void)setImgData:(NSArray *)array
{
collectionImageData = array;
[_myCollectionView reloadData];
}
@end


3.引用的时候为
IKEDMyOwnHorizenView *view = [[IKEDMyOwnHorizenView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 120)];
view.backgroundColor = [UIColor whiteColor];

NSArray *array = @[@{@"pic":@"1.jpg",@"title":@"A-1"},
@{@"pic":@"2.jpg",@"title":@"A-2"},
@{@"pic":@"3.jpg",@"title":@"A-3"},
@{@"pic":@"4.jpg",@"title":@"A-4"},
@{@"pic":@"3.jpg",@"title":@"A-3"},
@{@"pic":@"4.jpg",@"title":@"A-4"},
@{@"pic":@"3.jpg",@"title":@"A-3"},
@{@"pic":@"4.jpg",@"title":@"A-4"},
@{@"pic":@"3.jpg",@"title":@"A-3"},
@{@"pic":@"4.jpg",@"title":@"A-4"},
@{@"pic":@"5.jpg",@"title":@"A-5"}];

[view setImgData:array];
[self.view addSubview:view];


4.结果如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uiimage