由于代码量较大,本文主要简单讲解主要步骤,如有疑问可以留言或私信.
一,创建自定义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;
}