iOS oc 筛选页面(从右到左进入页面)

这篇博客详细介绍了如何在iOS应用中创建一个筛选页面,使用UICollectionView实现,每个section只允许选择一个条目,类似筛选条件。博客涵盖设置背景、初始化选择、布局配置、点击事件处理等内容,提供了一个实现多选约束的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在中uicollectionView  允许多选 但是每个section只许选择一个,类似于筛选条件的页面:



#import "selectVIew.h"

#import "CollectionCell.h" //自定义cell

#import "MyHeaderView.h" //自定义section header

@implementation selectVIew


/*

 // Only override drawRect: if you perform custom drawing.

 // An empty implementation adversely affects performance during animation.

 - (void)drawRect:(CGRect)rect {

 // Drawing code

 [UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1];

 }

 */

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        //手势

        [self setUI];

        [self selectGoods];

        [self bottomView];

        [self dissmissVIew];

    }

    return self;

}

#pragma mark --- 背景

- (void)setUI

{


    [self selectIndexPath];

    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(50*KWIDTH, 20*KWIDTH, Screen_W - 50*KWIDTH , Screen_H -40*KWIDTH)];

    backView.backgroundColor = [UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1];

    self.backView = backView;

    [self addSubview:backView];

 // 每组显示的内容

    self.appellationArr =[NSArray arrayWithObjects:@"不限", @"意大利",@"西班牙",@"澳大利亚",@"新西兰",@"智利",@"美国",@"中国",@"法国",@"印度",@"阿根廷",nil];

    self.typeArr = [NSArray arrayWithObjects:@"不限", @"红葡萄",@"红葡萄",@"红葡萄",@"红葡萄",@"红葡萄",@"红葡萄",@"红葡萄",@"红葡萄",@"红葡萄",@"红葡萄",@"印度",@"阿根廷",@"印度",@"阿根廷",@"印度",@"阿根廷",nil];

    self.breedArr = [NSArray arrayWithObjects:@"不限", @"茜拉",@"西班牙",@"澳大利亚",@"新西兰",@"智利",@"美国",@"中国",@"法国",@"印度",nil];

    self.priceArr =[NSArray arrayWithObjects:@"不限", @"0-100",@"100-150",@"150-200",nil];

    //标题内容

    self.headerArr =[NSArray arrayWithObjects:@"产区", @"类型",@"品种",@"价格",nil];

    self.collectionArr=[NSArray arrayWithObjects:self.appellationArr,self.typeArr,self.breedArr, self.priceArr,nil];

}

#pragma mark --- 初始化选择默认第一个被选中

-(void)selectIndexPath

{

    NSIndexPath  *indexPath =[[NSIndexPath alloc]init];

    self.cellDic =[[NSMutableDictionary alloc]init];

    [self.cellDic setValue:indexPath forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]];

#pragma mark --- 点击空白消失

}


-(void)dissmissVIew

{

   

    UIView *disView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50*KWIDTH,  Screen_H)];

    disView.alpha = 0.1;

    disView.backgroundColor = [UIColor whiteColor];

    [self addSubview:disView];

    UITapGestureRecognizer* singleRecognizer;

    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmiss)];

    singleRecognizer.numberOfTapsRequired = 1; // 单击

    [disView addGestureRecognizer:singleRecognizer];


}


#pragma mark --- 选酒


-(void)selectGoods

{

    UILabel *selectLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.backView.bounds.size.width, 50*KHEIGHT)];

    selectLabel.text = @"选酒";

    selectLabel.textColor =[UIColor whiteColor];

    selectLabel.backgroundColor =[UIColor colorWithRed:183.0/255.0 green:32.0/255.0 blue:34.0/255.0 alpha:1];

    selectLabel.textAlignment = NSTextAlignmentCenter;

    self.selectLabel = selectLabel;

    [self collectionAction];

  

    [self.backView addSubview:selectLabel];

}

#pragma mark --- 消失事件

-(void)dissmiss

{

    [UIView animateWithDuration:0.25 animations:^{

        [self removeFromSuperview];

    }];

    

}

#pragma mark --- collection

-(void)collectionAction

{

    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];

    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,50*KHEIGHT, _backView.bounds.size.width, _backView.bounds.size.height-100*KHEIGHT)collectionViewLayout:flowLayout];

    self.collectionView.backgroundColor = [UIColor whiteColor];

    //注册

    [flowLayout setHeaderReferenceSize:CGSizeMake(self.collectionView.frame.size.width, 30*KHEIGHT)];

#pragma mark -- 注册头部视图

    [self.collectionView registerClass:[MyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

    [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"cell"];

    //设置代理

    self.collectionView.delegate = self;

    self.collectionView.dataSource = self;

    self.collectionView.allowsMultipleSelection = YES;

    [self.backView addSubview:self.collectionView];

    

}

#pragma mark - collectionView delegate 协议

//设置分区


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView{

    

    return 4;

}



//每个分区上的元素个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    

    if (section == 1) {

        return self.typeArr.count;

    }

    if (section == 2) {

        

        return self.breedArr.count;

    }

    if (section == 3) {

        

        return self.priceArr.count;

    }

    

    return self.appellationArr.count;

    

}


//设置元素内容

- (UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *identify = @"cell";

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];

    [cell sizeToFit];

    if (!cell) {

        

    }

   

    cell.backgroundColor = [UIColor whiteColor];

    cell.titleLabel.textColor =[UIColor blackColor];

    //将字典中元素提取,确认第几个被选中

    NSIndexPath  *index =[self.cellDic valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]];

    

    if (indexPath.row == index.row)

    {

        cell.backgroundColor = [UIColor blueColor];

        cell.titleLabel.textColor =[UIColor whiteColor];

     

    }

    else

    {

        cell.backgroundColor = [UIColor whiteColor];

        cell.titleLabel.textColor =[UIColor blackColor];

    }

    

    

    cell.layer.borderColor=[UIColor darkGrayColor].CGColor;

    cell.layer.borderWidth=0.3;


    cell.titleLabel.text =[NSString stringWithFormat:@"%@",[[self.collectionArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];

    

    return cell;

}


//设置元素的的大小框

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    UIEdgeInsets top = {6*KWIDTH,6*KHEIGHT,6*KWIDTH,6*KHEIGHT};

    return top;

}


////设置顶部的大小

//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

//    CGSize size={5*KWIDTH,5*KHEIGHT};

//    return size;

//}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(90*KWIDTH, 25*KHEIGHT);

}


//点击元素触发事件

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    

    

    NSIndexPath *cellIndex = [self.cellDic valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]];

    //确定一个section 只有一个被选中若两个cell在同一个section中时移除前一个,将后点击的加入字典当中

    if (indexPath.section == cellIndex.section )

    {

       

        [self.cellDic removeObjectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]];

    }

   

    [self.cellDic setValue:indexPath forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]];

//刷新点击的section

    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:indexPath.section];

    //取消section 刷新式的闪烁

    [UIView animateWithDuration:0 animations:^{

        [self.collectionView performBatchUpdates:^{

               [self.collectionView reloadSections:indexSet];

        } completion:nil];

    }];

    

 

  


    

}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    

    UICollectionReusableView *reusableView = nil;

    

    if (kind == UICollectionElementKindSectionHeader) {

        //定制头部视图的内容

        MyHeaderView *headerV = (MyHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        headerV.titleLab.text = [self.headerArr objectAtIndex:indexPath.section];

        reusableView = headerV;

    }

    

    return reusableView;

}

#pragma mark --- 底部

-(void)bottomView

{

    self.bottom =[[UIView alloc]initWithFrame:CGRectMake(0,580*KHEIGHT ,  self.backView.bounds.size.width, self.backView.bounds.size.height - 585*KHEIGHT)];

    self.bottom.backgroundColor = [UIColor redColor];

    

    

    self.backButton =[[UIButton alloc]initWithFrame:CGRectMake(0,0self.bottom.bounds.size.width/3self.bottom.bounds.size.height)];

    [self.backButton setTitle:@"返回" forState:UIControlStateNormal];

    [self.backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [self.backButton addTarget:self action:@selector(dissmiss) forControlEvents:UIControlEventTouchDown];

    self.backButton.backgroundColor = ODOOBackgroundColor;

    [self.bottom  addSubview:self.backButton];

    

    

    self.resettingButton =[[UIButton alloc]initWithFrame:CGRectMake(self.bottom.bounds.size.width/3,0self.bottom.bounds.size.width/3self.bottom.bounds.size.height)];

    [self.resettingButton setTitle:@"重置" forState:UIControlStateNormal];

    [self.resettingButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [self.resettingButton addTarget:self action:@selector(resetting) forControlEvents:UIControlEventTouchDown];

    self.resettingButton.backgroundColor = ODOOBackgroundColor;

    [self.bottom  addSubview:self.resettingButton];

    

    

    self.selectButton =[[UIButton alloc]initWithFrame:CGRectMake(self.bottom.bounds.size.width/3*2,0self.bottom.bounds.size.width/3self.bottom.bounds.size.height+5*KHEIGHT)];

    [self.selectButton setTitle:@"筛选" forState:UIControlStateNormal];

    [self.selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    self.selectButton.backgroundColor = [UIColor redColor];

    [self.selectButton addTarget:self action:@selector(selectAction) forControlEvents:UIControlEventTouchDown];

    [self.bottom  addSubview:self.selectButton];

    

    

    [self.backView  addSubview:self.bottom];


    

}

#pragma mark -- 筛选

-(void)selectAction

{

 

     NSArray *appllationArray = [[NSArray alloc]init];

     NSArray *typeArray = [[NSArray alloc]init];

     NSArray *breedArray = [[NSArray alloc]init];

     NSArray *priceArray = [[NSArray alloc]init];

    

    NSIndexPath *appellationIndexPath = [self.cellDic valueForKey:@"0"];

    NSIndexPath *typeIndexPath = [self.cellDic valueForKey:@"1"];

    NSIndexPath *breedIndexPath = [self.cellDic valueForKey:@"2"];

    NSIndexPath *priceIndexPath = [self.cellDic valueForKey:@"3"];

    

    if (appellationIndexPath ==nil)

    {

        appllationArray =[_appellationArr objectAtIndex:0];

    }

    else

    {

        appllationArray = [self.appellationArr objectAtIndex:appellationIndexPath.row];

    }


    

    

    if (typeIndexPath ==nil)

    {

        typeArray =[_appellationArr objectAtIndex:0];

    }

    else

    {

        typeArray = [self.appellationArr objectAtIndex:appellationIndexPath.row];

    }

    

    

    

    if (breedIndexPath ==nil)

    {

        breedArray =[_appellationArr objectAtIndex:0];

    }

    else

    {

        breedArray = [self.appellationArr objectAtIndex:appellationIndexPath.row];

    }

    

    

    

    if (priceIndexPath ==nil)

    {

        priceArray =[_appellationArr objectAtIndex:0];

    }

    else

    {

        priceArray = [self.appellationArr objectAtIndex:appellationIndexPath.row];

    }

    

    

    

    

    

     NSArray *selectArray =[NSArray arrayWithObjects:appllationArray,typeArray,breedArray,priceArray,nil];

  

     NSLog(@"selectAction ====  %@",self.cellDic);

     NSLog(@"selectArray ====  %@",selectArray);

    

  

}

#pragma mark -- 重置

-(void)resetting

{

    [self.cellDic removeAllObjects];

    [self selectIndexPath];

    [self.collectionView reloadData];


}



@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值