我们的需求是,分组,每组单选。
首先创建collectionView
/** collectionView */
@property (nonatomic, strong) UICollectionView *collectionView;
/** layout */
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic ,weak) UIButton *btn;
@property (nonatomic ,strong) NSArray *productTypeArr; //产品类型
@property (nonatomic ,strong) NSArray *levelArr; //风险等级;
@property (nonatomic ,strong) NSArray *amountArr; //起投金额
@property (nonatomic ,strong) NSMutableArray *dataArr;
@property (nonatomic ,strong) NSMutableArray *pSelectArr;
@property (nonatomic ,strong) NSMutableArray *leveSelecArr;
@property (nonatomic ,strong) NSMutableArray *amountSelecArr;
@property (nonatomic ,assign) NSInteger pIndexRow;
@property (nonatomic ,assign) NSInteger leveIndexRow;
@property (nonatomic ,assign) NSInteger amounIndexRow;
self.layout = [[UICollectionViewFlowLayout alloc] init];
self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth;
self.layout.itemSize = CGSizeMake(CGFloatAutoFitX(84), CGFloatAutoFitY(27));
self.layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
self.layout.minimumLineSpacing = 6;
self.layout.minimumInteritemSpacing = 5;
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.scrollEnabled = NO;
[TSTMemuCCell registCollectionViewCellWith:self.collectionView];
[self.collectionView registerClass:[TSTMemuHeardView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader"];
[self addSubview:self.collectionView];
然后添加重置和完成按钮
UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGFloatAutoFitX(12), CGFloatAutoFitY(364) - CGFloatAutoFitY(12) - 46, CGFloatAutoFitX(168), 46)];
[resetBtn setTitle:@"重置" forState:0];
resetBtn.titleLabel.font = TSTFont(18);
[resetBtn setTitleColor:HEXRGBCOLOR(0x4A4A4A) forState:UIControlStateNormal];
resetBtn.layer.borderColor = HEXRGBCOLOR(0xEEEEEE).CGColor;
resetBtn.layer.borderWidth = 1.0;
resetBtn.layer.cornerRadius = 4;
[resetBtn addTarget:self action:@selector(resetClick) forControlEvents:UIControlEventTouchUpInside];
[self.collectionView addSubview:resetBtn];
UIButton *completeBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(resetBtn.frame) + CGFloatAutoFitX(15), CGRectGetMinY(resetBtn.frame), CGFloatAutoFitX(168), 46)];
[completeBtn setTitle:@"完成" forState:0];
completeBtn.titleLabel.font = TSTFont(18);
completeBtn.backgroundColor = HEXRGBCOLOR(0xCA4B46);
completeBtn.layer.cornerRadius = 4;
[completeBtn addTarget:self action:@selector(completeClick) forControlEvents:UIControlEventTouchUpInside];
[self.collectionView addSubview:completeBtn];
collectionView的分组和tableview不一样。他的每个组头都是一个UIView需要自己创建一下。并不像table一样有自带的。
创建collectionView的组头。
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.labTitle = [[UILabel alloc] init];
self.labTitle.frame = CGRectMake(CGFloatAutoFitX(12), 14, 120, 15);
self.labTitle.font = TSTFont(12);
self.labTitle.textColor = HEXRGBCOLOR(0x6F6F6F);
[self addSubview:self.labTitle];
}
return self;
}
记得写上这句。后面的identifier是固定的,直接抄就行。
[self.collectionView registerClass:[TSTMemuHeardView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader"];
//头部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
TSTMemuHeardView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionElementKindSectionHeader" forIndexPath:indexPath];
if (indexPath.section == 0) {
headView.labTitle.text = @"产品类型";
} else if (indexPath.section == 1) {
headView.labTitle.text = @"风险等级";
}else{
headView.labTitle.text = @"起头金额(元)";
}
return headView;
}
//加载collection
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
TSTMemuCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[TSTMemuCCell identifier] forIndexPath:indexPath];
if (indexPath.section == 0) {
TSTQueryProductTypeModel *model = self.dataArr[indexPath.section][indexPath.row];
cell.lable.text = model.productTypeName;
cell.tag = model.productType;
}else{
cell.lable.text = self.dataArr[indexPath.section][indexPath.row];
}
return cell;
}
OK到这里视图基本写好了。
下面开始最最重要的核心代码了
collection的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
TSTMemuCCell *cell = (TSTMemuCCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.lable.backgroundColor = HEXRGBCOLOR(0xFFE8E7);
cell.lable.textColor = HEXRGBCOLOR(0xCA4B46);
if (indexPath.section == 0) {
[self.pSelectArr addObject:cell];
if (self.pSelectArr.count > 1 && self.pIndexRow != indexPath.row) {
[self dselection:indexPath isSame:NO];
}else if (self.pSelectArr.count > 1 && self.pIndexRow == indexPath.row){
[self dselection:indexPath isSame:YES];
}else if (self.pSelectArr.count == 1 && self.pIndexRow == indexPath.row){
cell.lable.backgroundColor = HEXRGBCOLOR(0xFFE8E7);
cell.lable.textColor = HEXRGBCOLOR(0xCA4B46);
}else if (self.pSelectArr.count == 1 && self.pIndexRow != indexPath.row) {
NSIndexPath *idx = [NSIndexPath indexPathForRow:self.pIndexRow inSection:0];
TSTMemuCCell *cc = (TSTMemuCCell *)[collectionView cellForItemAtIndexPath:idx];
[self collectionViewCellColorWith:cc];
self.pIndexRow = indexPath.row;
}
}
if (indexPath.section == 1) {
[self.leveSelecArr addObject:cell];
if (self.leveSelecArr.count > 1 && self.leveIndexRow != indexPath.row) {
[self dselection:indexPath isSame:NO];
}else if (self.leveSelecArr.count > 1 && self.leveIndexRow == indexPath.row){
[self dselection:indexPath isSame:YES];
}else if (self.leveSelecArr.count == 1 && self.leveIndexRow == indexPath.row){
cell.lable.backgroundColor = HEXRGBCOLOR(0xFFE8E7);
cell.lable.textColor = HEXRGBCOLOR(0xCA4B46);
}else if (self.leveSelecArr.count == 1 && self.leveIndexRow != indexPath.row){
NSIndexPath *idx = [NSIndexPath indexPathForRow:self.leveIndexRow inSection:1];
TSTMemuCCell *cc = (TSTMemuCCell *)[collectionView cellForItemAtIndexPath:idx];
[self collectionViewCellColorWith:cc];
self.leveIndexRow = indexPath.row;
}
}
if (indexPath.section == 2) {
[self.amountSelecArr addObject:cell];
if (self.amountSelecArr.count > 1 && self.amounIndexRow != indexPath.row) {
[self dselection:indexPath isSame:NO];
}else if (self.amountSelecArr.count > 1 && self.amounIndexRow == indexPath.row){
[self dselection:indexPath isSame:YES];
}else if (self.amountSelecArr.count == 1 && self.amounIndexRow == indexPath.row) {
cell.lable.backgroundColor = HEXRGBCOLOR(0xFFE8E7);
cell.lable.textColor = HEXRGBCOLOR(0xCA4B46);
}else if (self.amountSelecArr.count == 1 && self.amounIndexRow != indexPath.row){
NSIndexPath *idx = [NSIndexPath indexPathForRow:self.amounIndexRow inSection:2];
TSTMemuCCell *cc = (TSTMemuCCell *)[collectionView cellForItemAtIndexPath:idx];
[self collectionViewCellColorWith:cc];
self.amounIndexRow = indexPath.row;
}
}
}
其实这个点击事件一共分二种情况
1、点击的下一个和上一个不同
2、点击的下一个和上一个相同(也就是第二次点击取消状态)
但是我是通过数组,每次点击一个放到数组里,就会让数组里,每次都保持两个。
下面是点击取消状态。
//取消选定
-(void)dselection:(NSIndexPath *)index isSame:(BOOL)same{
if (index.section == 0 && same == NO) {
TSTMemuCCell *cell = [self.pSelectArr firstObject];
[self collectionViewCellColorWith:cell];
[self.pSelectArr removeObjectAtIndex:0];
self.pIndexRow = index.row;
}else if (index.section == 0 && same == YES){
TSTMemuCCell *cell = [self.pSelectArr firstObject];
[self.pSelectArr removeAllObjects];
[self collectionViewCellColorWith:cell];
self.pIndexRow = index.row;
}
if (index.section == 1 && same == NO) {
TSTMemuCCell *cell = self.leveSelecArr[0];
[self collectionViewCellColorWith:cell];
[self.leveSelecArr removeObjectAtIndex:0];
self.leveIndexRow = index.row;
}else if (index.section == 1 && same == YES){
TSTMemuCCell *cell = [self.leveSelecArr firstObject];
[self.leveSelecArr removeAllObjects];
[self collectionViewCellColorWith:cell];
self.leveIndexRow = index.row;
}
if (index.section == 2 && same == NO) {
TSTMemuCCell *cell = [self.amountSelecArr firstObject];
[self collectionViewCellColorWith:cell];
[self.amountSelecArr removeObjectAtIndex:0];
self.amounIndexRow = index.row;
}else if (index.section == 2 && same == YES){
TSTMemuCCell *cell = [self.amountSelecArr firstObject];
[self.amountSelecArr removeAllObjects];
[self collectionViewCellColorWith:cell];
self.amounIndexRow = index.row;
}
}
以上就是最核心的代码了。
剩下的就是一些代理方法了。
完成就是把三个数组里的内容,传递到控制器里。
重置就是,数组全部清空,按钮灰色就好了。
有什么不懂得可以发给我留言。或者指点我有没有更好的办法。