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.结果如下
我在我自己定义的一个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 UICollectionView手写代码实现步骤
- iOS_UIScrollView实现无限滚动,思路与代码
- IOS 瀑布流UICollectionView实现
- iOS UICollectionView 实现轮播图
- iOS 实现UICollectionView头部悬停效果
- IOS瀑布流通过UICollectionView控件实现
- iOS 6开发---应用集合视图(UICollectionView)-实现补充视图(Supplementary View)
- collectionRepeat实现的类似IOS的UICollectionView
- [iOS]分享一段用UITouch事件来实现View的旋转缩放移动的核心代码
- iOS中UIScrollView、UIWebView、UICollectionView实现图文混排
- iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码
- iOS 手写代码UICollectionView
- iOS下用一行代码实现tableview(collectionview)的parallax效果
- iOS tableView实现单选和多选的实例代码
- iOS项目开发实战——实现UICollectionView的动态增加Cell与Section
- iOS UICollectionView 按钮点击变色(收藏点赞功能)实现
- iOS自定义UICollectionViewFlowLayout实现图片浏览效果
- (2)iOS用UICollectionView实现Gallery效果
- iOS Swift利用UICollectionView实现无限轮播功能(原理)详解
- iOS 利用UICollectionView横向滚动、余弦函数曲线特性实现居中放大的卡片浏览工具 XLCardSwitch