iOS CollectionView实现屏幕左右滑动

由于代码量较大,本文主要简单讲解主要步骤,如有疑问可以留言或私信.

一,创建自定义UICollectionViewCell,并实现TableView的数据源,代理方法.

#import <UIKit/UIKit.h>

@interface AKCollectionCellFirst : UICollectionViewCell<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tabView;

@end


#import "AKCollectionCellFirst.h"


@interface AKCollectionCellFirst() 


@end
@implementation AKCollectionCellFirst

- (id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self){
        
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tabView.delegate = self;
        _tabView.dataSource = self;
        //如果需要自定义tableViewCell可以在此对其进行注册
        [_tabView registerClass:[AKTableCellFirst class] forCellReuseIdentifier:@"ONE"];
       
        
        [self addSubview:_tabView];
    }
    
    return self;
}

二, 在CollectionViewCell中实现TableView的数据源和代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AKTableCellFirst *cell = [tableView dequeueReusableCellWithIdentifier:@"ONE"];
    
    if (cell == nil){
        
        cell = [[AKTableCellFirst alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ONE"];
    }
    
    NSArray *arrayP = @[@"Kuture",@"CD",@"ER",@"TE",@"WEB",@"AUI",@"HG",@"TYT",@"FHB",@"AW"];
    
    cell.person.text = arrayP[indexPath.row];
    cell.time.text = @"2016-07-11 15:08";
    cell.flowChat.text = @"What?";
    cell.content.text = @"You Know Who I am And you Find SomeThing,What?You find again ? I do not Belive that";
    cell.content.numberOfLines = 0;
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

 

三, 创建UIViewController控制器并继承Collection协议与代理方法,创建UICollectionView,并注册自定义CollectionViewCell

#import "AKCollection.h"

@interface AKCollection()<UICollectionViewDelegate,
                         UICollectionViewDelegateFlowLayout,
                         UICollectionViewDataSource>
@end

@implementation AKCollection

- (void)viewDidLoad{
   
  [super viewDidLoad];
  
  UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(SCREEN_W, SCREEN_H - 150);
    flowLayout.minimumLineSpacing = 0;

    
    UICollectionView *collec = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    _collec = collec;
    _collec.delegate = self;
    _collec.dataSource = self;
    _collec.bounces = NO;
    _collec.backgroundColor = GROUND_COLOR(242, 242, 242, 1);
    _collec.alpha = 0.95;
    _collec.pagingEnabled = YES;
    
    //注册自定义的UICollectionViewCell
    [_collec registerClass:[AKCollectionCellFirst class] forCellWithReuseIdentifier:@"CellFirst"];
    [_collec registerClass:[AKCollectCellSecond class] forCellWithReuseIdentifier:@"CellSecond"];
    [_collec registerClass:[AKCollectCellThird class] forCellWithReuseIdentifier:@"CellThird"];
    [_collec registerClass:[AKCollectCellFourth class] forCellWithReuseIdentifier:@"CellFourth"];
    
    [self.view addSubview:_collec];

}

四, 实现数据源,代理方法:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell;
    
    NSArray *idArray = @[@"CellFirst",@"CellSecond",@"CellThird",@"CellFourth"];
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:idArray[indexPath.item] forIndexPath:indexPath];

    return cell;

}

 

转载于:https://my.oschina.net/Kuture/blog/718893

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值