多数据对比,,上下左右滑动对比数据
要实现的功能是对几种车型进行比对,,参数多可以上下左右滑动对比。。
整体思路: 首先头部显示汽车名称的控件是一个collectionView,,滑动方向是左右 大的控件是tableView 带分区,,每个tableViewCell 里创建一个横向滑动的collectionView,,这样就能上下划tableview 左右划collectionview ,,做过类似嵌套的肯定会想到。。这样做当左右滑动一个collectionview的时候 其他tableviewcell 里面的collectionview不会同步滑动,,所以需要创建一个单利数组 添加所有的新建的collectionview 包括头部的汽车名称的collectionview 当触发滑动方法是将contentOffset 设置同步就好 下面贴出一些关键的代码
创建一对单利文件
.h 文件中添加
#import <Foundation/Foundation.h>
@interface DuiBiSingleTon :NSObject
+(DuiBiSingleTon *)shareSingleton;
// 用于存储同一滑动的collectionview的数组
@property (nonatomic,strong)NSMutableArray *duibiArray;
@end
.m文件中添加方法
#import "DuiBiSingleTon.h"
@implementation DuiBiSingleTon
+(DuiBiSingleTon *)shareSingleton{
staticDuiBiSingleTon *singleton;
staticdispatch_once_t oneToken;
dispatch_once(&oneToken, ^{
singleton = [[DuiBiSingleTonalloc]init];
});
return singleton;
}
@end
- (void)viewDidLoad {
[DuiBiSingleTonshareSingleton].duibiArray = [NSMutableArrayarray];
}
- (void)createCollectionView{
。。。
//
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayoutalloc]init];
flow.scrollDirection =UICollectionViewScrollDirectionHorizontal;
flow.itemSize =CGSizeMake(120,64);
flow.minimumLineSpacing =0;
flow.minimumInteritemSpacing =0;
self.titleCollectionView = [[UICollectionViewalloc]initWithFrame:CGRectMake(60,0,WIDTH-60,50)collectionViewLayout:flow];
self.titleCollectionView.showsHorizontalScrollIndicator = NO;
[self.titleCollectionViewregisterClass:[titleCollectionViewCellclass]forCellWithReuseIdentifier:@"titleReuse"];
[headView addSubview:self.titleCollectionView];
self.titleCollectionView.backgroundColor = [UIColorwhiteColor];
self.titleCollectionView.delegate = self;
self.titleCollectionView.dataSource = self;
// 将这个显示车名的collectionview 添加到单利数组中
[[DuiBiSingleTonshareSingleton].duibiArrayaddObject:self.titleCollectionView];
}
// 主页面汽车名称collectionview 滑动的时候触发方法,,设置所有的collectionview相同偏移
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView ==self.titleCollectionView) {
for (UICollectionView *viewin [DuiBiSingleTonshareSingleton].duibiArray) {
view.contentOffset = scrollView.contentOffset;
}
}
}
tableviewCell 中嵌套
- (void)createView{
。。。
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayoutalloc]init];
flow.itemSize =CGSizeMake(120,63);
flow.minimumLineSpacing =0;
flow.minimumInteritemSpacing =0;
flow.scrollDirection =UICollectionViewScrollDirectionHorizontal;
self.duibiCollectionView = [[UICollectionViewalloc]initWithFrame:CGRectZerocollectionViewLayout:flow];
[self.contentViewaddSubview:self.duibiCollectionView];
self.duibiCollectionView.backgroundColor = [UIColorwhiteColor];
[self.duibiCollectionViewregisterClass:[DuiBiCollectionViewCellclass]forCellWithReuseIdentifier:@"cellReuse"];
self.duibiCollectionView.showsHorizontalScrollIndicator = NO;
self.duibiCollectionView.delegate = self;
self.duibiCollectionView.dataSource = self;
/// 将每个cell中的collectionview添加到单利数组中
[[DuiBiSingleTonshareSingleton].duibiArrayaddObject:self.duibiCollectionView];
}
/// tableviewcell 中的collectionview滑动的时候 同步所有的collectionview的偏移量
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView ==self.duibiCollectionView) {
for (UICollectionView *viewin [DuiBiSingleTonshareSingleton].duibiArray) {
view.contentOffset = scrollView.contentOffset;
}
}
}
具体参考工程代码
百度云盘
链接: https://pan.baidu.com/s/1boMFjLd 密码: 46f2