重要的寫在前面童番,寫了一個(gè)tabelVew嵌套CollectionView的使用方法锅尘,相關(guān)的demo請(qǐng)點(diǎn)擊demo鏈接?
簡(jiǎn)單的說顷蟆,就是每一個(gè)tabelVewCell里面都包含一個(gè)CollectionView.
其實(shí)思路很簡(jiǎn)單的:
1.創(chuàng)建一個(gè)tableVIew
加入數(shù)據(jù)源修壕,加入tableView如果需要加入一個(gè)頭視圖,加入代理方法
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, SCREEN_HEIGHT)];
_tableView.backgroundColor =[UIColor whiteColor];
_tableView.dataSource =self;
_tableView.delegate = self;
[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
_tableView.showsVerticalScrollIndicator = NO;
[_tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"HomeTableViewCell"];
UIImageView *headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
headerView.image = [UIImage imageNamed:@"風(fēng)景1.jpg"];
_tableView.tableHeaderView = headerView;
[_tableView reloadData];
}
return _tableView;
}
#pragma mark -? tableViewData 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
2.自定義tableVIewCell,每個(gè)tableVIewCell里面搞個(gè)collectionVIew
//在cell的初始化方法里面搞一個(gè)collectionVIew
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
}
//在layoutSubviews計(jì)算每個(gè)tableVIewCell的collectionVIew的大小
- (void)layoutSubviews{
[super layoutSubviews];
}
#pragma mark -- Collection delegate
//這是UICollectionView的點(diǎn)擊方法损同,如果點(diǎn)擊每個(gè)UICollectionViewCell需要在viewController里面響應(yīng)的話翩腐。需要自定義一個(gè)代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if([self.delegate respondsToSelector:@selector(CustomCollection:didSelectRowAtIndexPath:str:)]){
[self.delegate CustomCollection:collectionView didSelectRowAtIndexPath:indexPath str:self.collectDataArray[indexPath.row]];
}}
3.自定義一下collectionVIewCell
自定義collectionVIewCell
需要計(jì)算一下根據(jù)屏幕的寬度,沒行可以顯示幾個(gè)
總結(jié):有很多的項(xiàng)目中tabelVew嵌套CollectionView其實(shí)每個(gè)cell就是一行CollectionView膏燃,例如AppStore茂卦,這種只要設(shè)置CollectionView水平方向滑動(dòng)就好了。
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
在iOS的開發(fā)過程中组哩,如有問題可以與我聯(lián)系等龙!
郵箱:2877025939@qq.com