IOS之UICollectionView初探(代码实现)

本文介绍了一个自定义的UICollectionViewCell类,用于展示水平滚动的图片集合视图。通过创建IKEDMyOwnHorizenView类,实现了UICollectionView的数据源和代理方法,并展示了如何注册自定义的UICollectionViewCell类和设置图片数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值