前言
在實際項目中我們有時候會遇到這樣的需求,列表中包含多個模型數據一行顯示不下,需要左右滑動cell或者換行上下顯示出來,這里我們可以想到使用tableView嵌套collectionView來實現功能
tableView嵌套collectionView.gif
我這里介紹一下橫向滾動collectionView
一 、創建tableView,設置代理不多說
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height ) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
self.tableView = tableView;
[self.view addSubview: self.tableView];
實現代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HZTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HZTableViewCell"];
if (cell == nil) {
cell = [[HZTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HZTableViewCell"];
cell.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row+1];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
}
二、自定義tableViewCell
cell上添加一個collectionView,并成為其代理
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifie{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifie]) {
[self setView]; }
return self;
}
-(void)setView{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.minimumLineSpacing = 40;
//橫向滑動
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(50, 0, [UIScreen mainScreen].bounds.size.width-50, 200) collectionViewLayout:layout];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.backgroundColor = [UIColor purpleColor];
[self.contentView addSubview:collectionView];
[collectionView registerClass:[HZCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
實現代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
HZCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",indexPath.row+1]];
cell.label.text =[NSString stringWithFormat:@"%zd. %@",indexPath.row+1,self.dataSource[indexPath.row]];
return cell;
}
要實現點擊某個item將item所在的位置傳給控制器以方便做事情(我采用的是代理的方法)
定義一個協議
#import <Foundation/Foundation.h>
@class HZTableViewCell;
@protocol HZCollectionViewDelegate <NSObject>
- (void)hzcollectionView:(HZTableViewCell *)hzTableViewCell didSelectItemAtIndexPath:(NSIndexPath *)collectionView;
@end
在collectionView的點擊事件中執行代理方法,所以在上邊創建 HZTableViewCell的時候我有寫到 cell.delegate = self;
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (_delegate) {
[_delegate hzcollectionView:self didSelectItemAtIndexPath:indexPath];
}
}
然后需要在控制器中實現代理方法
-(void)hzcollectionView:(HZTableViewCell *)hzTableViewCell didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSIndexPath *tableViewIndexPath = [self.tableView indexPathForCell:hzTableViewCell];
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"點擊了第%ld行的第%ld個",tableViewIndexPath.row+1,indexPath.row+1] preferredStyle:UIAlertControllerStyleAlert];
[self.navigationController presentViewController:alertVC animated:YES completion:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alertVC dismissViewControllerAnimated:YES completion:^{
}];
});
}];
}