IOS之UICollectionView初探(代码实现)
发布日期:2021-11-21 04:40:55 浏览次数:24 分类:技术文章

本文共 3393 字,大约阅读时间需要 11 分钟。

自定义一个类,用于展现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 
@interface IKEDMyOwnHorizenView : UIView
@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.结果如下

转载地址:https://blog.csdn.net/xiaoji94shen/article/details/20048003 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:IOS自定义View实现相应的控件点击方法以及代理的总结(附代码)
下一篇:IOS自定义View

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月18日 07时09分06秒