您的位置:首页 > 移动开发 > IOS开发

iOS CollectionView实现屏幕左右滑动

2016-07-27 00:00 423 查看
摘要: 将UITableView嵌入到UICollectionViewCell中,一个CollectionViewCell即为一个屏幕,在Cell中嵌入TableView用来显示将要展示的界面或数据

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

一,创建自定义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:[A
3ff0
KCollectCellThird 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;

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