tabelVew嵌套CollectionView

重要的寫(xiě)在前面,寫(xiě)了一個(gè)tabelVew嵌套CollectionView的使用方法,相關(guān)的demo請(qǐng)點(diǎn)擊demo鏈接?


簡(jiǎn)單的說(shuō),就是每一個(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ù)屏幕的寬度,沒(méi)行可以顯示幾個(gè)



總結(jié):有很多的項(xiàng)目中tabelVew嵌套CollectionView其實(shí)每個(gè)cell就是一行CollectionView,例如AppStore,這種只要設(shè)置CollectionView水平方向滑動(dòng)就好了。

layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

在iOS的開(kāi)發(fā)過(guò)程中,如有問(wèn)題可以與我聯(lián)系!

郵箱:2877025939@qq.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容