iOS 筛选界面

类似于这种

我们的需求是,分组,每组单选。
首先创建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;
    }
}

以上就是最核心的代码了。
剩下的就是一些代理方法了。
完成就是把三个数组里的内容,传递到控制器里。
重置就是,数组全部清空,按钮灰色就好了。

有什么不懂得可以发给我留言。或者指点我有没有更好的办法。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345