iOS记录

UICollectionView单选多选的实现

2018-06-26  本文已影响0人  Coder_di

今天收到一个页面,是要实现简单的单选,多选,重置等功能,在网上找了夏没找到合适的,然后自己就造了一个轮子,哈哈。。。效果如下,不喜勿喷~~~


废话不多说,具体代码如下:

#import "ShaiXuanViewController.h"

#import "ShaiXuanCell.h"

@interface ShaiXuanViewController ()

@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)UIButton *selectedBtn;

@property (nonatomic, strong)NSMutableArray *dataArray;

@property (nonatomic, strong)NSMutableArray *selectedArray;

@property (nonatomic, strong)UIButton *chongzhiBtn;

@property (nonatomic, strong)UIButton *okBtn;

@end

@implementationShaiXuanViewController

-(void)viewWillAppear:(BOOL)animated{

    [superviewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:NO];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title=@"筛选";

    self.view.backgroundColor = [UIColor whiteColor];

    _selectedArray = [NSMutableArray array];

    _dataArray=  [NSMutableArray arrayWithObjects:@"经济",@"法规",@"管理",@"建筑实务",@"机电实务",@"公路实物",@"好的", nil];

    [selfsetUI];

     [self setcollectionView];

-(void)setUI{

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, 5 * KscreenHeight)];

    [self.view addSubview:view];

    UILabel*lable = [UILabelnew];

    lable.text=@"科目";

    lable.font = [UIFont systemFontOfSize:16 * KscreenWidth];

    [self.viewaddSubview:lable];

    lable.sd_layout

    .topSpaceToView(view,20*KscreenHeight)

    //    .rightSpaceToView(self.view, 0)

    .leftSpaceToView(self.view, 20 * KscreenWidth)

    .widthIs(40 * KscreenWidth)

    .heightIs(20 * KscreenHeight);

    _selectedBtn = [UIButton new];

    [_selectedBtn setBackgroundColor:[UIColor whiteColor]];

    [_selectedBtn setBackgroundImage:[UIImage imageNamed:@"User_choose"] forState:UIControlStateSelected];

    [_selectedBtn setBackgroundImage:[UIImage imageNamed:@"User_selected"] forState:0];

    [_selectedBtn addTarget:self action:@selector(quanxuanAction) forControlEvents:(UIControlEventTouchUpInside)];

    [_selectedBtn setTitleColor:ZhuTiColor forState:UIControlStateNormal];

    [self.view addSubview:_selectedBtn];

    _selectedBtn.sd_layout

    .topSpaceToView(view,20*KscreenHeight)

    .leftSpaceToView(lable,10*KscreenWidth)

    .widthIs(20 * KscreenWidth)

    .heightIs(20 * KscreenHeight);

UILabel *lable1 = [UILabel new];

    lable1.text=@"全选";

    lable1.font = [UIFont systemFontOfSize:16 * KscreenWidth];

    [self.viewaddSubview:lable1];

    lable1.sd_layout

    .topSpaceToView(view,20*KscreenHeight)

    //    .rightSpaceToView(self.view, 0)

    .leftSpaceToView(_selectedBtn, 10 * KscreenWidth)

    .widthIs(40 * KscreenWidth)

    .heightIs(20 * KscreenHeight);

    _chongzhiBtn = [UIButton new];

    _chongzhiBtn.layer.masksToBounds = YES;

    _chongzhiBtn.layer.cornerRadius = 5;

    _chongzhiBtn.layer.borderColor = ZhuTiColor.CGColor;

    _chongzhiBtn.layer.borderWidth = 1;

    [_chongzhiBtn setTitle:@"重置" forState:0];

    [_chongzhiBtn addTarget:self action:@selector(chongzhiAction) forControlEvents:(UIControlEventTouchUpInside)];

    [_chongzhiBtn setTitleColor:ZhuTiColor forState:UIControlStateNormal];

    [self.view addSubview:_chongzhiBtn];

    _chongzhiBtn.sd_layout

    .bottomSpaceToView(self.view,50 * KscreenHeight)

    .leftSpaceToView(self.view, 20 * KscreenWidth)

    .widthIs((ScreenWidth - 60 * KscreenWidth)/2)

    .heightIs(50 * KscreenHeight);

    _okBtn = [UIButton new];

    _okBtn.layer.masksToBounds = YES;

    _okBtn.layer.cornerRadius = 5;

    _okBtn.backgroundColor = ZhuTiColor;

    [_okBtn setTitle:@"提交" forState:0];

    [_okBtn addTarget:self action:@selector(okAction) forControlEvents:(UIControlEventTouchUpInside)];

    [self.view addSubview:_okBtn];

    _okBtn.sd_layout

    .bottomSpaceToView(self.view,50 * KscreenHeight)

    .rightSpaceToView(self.view, 20 * KscreenWidth)

    .widthIs((ScreenWidth - 60 * KscreenWidth)/2)

    .heightIs(50 * KscreenHeight);

}

//取消

-(void)chongzhiAction{

     _selectedArray = [NSMutableArray array];

    [self.collectionView reloadData];

}

//确定按钮

-(void)okAction{

}

//全选勾选事件

-(void)quanxuanAction{

    _selectedBtn.selected = !_selectedBtn.selected;

    if(_selectedBtn.selected==YES) {

        _selectedArray = _dataArray.mutableCopy;

    }else{

        _selectedArray = [NSMutableArray array];

    }

     [self.collectionView reloadData];

}

-(void)setcollectionView{

    //设置布局对象

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

    //设置item大小

    flowLayout.itemSize=CGSizeMake(100*KscreenWidth,30*KscreenHeight);

    //设置最小列间距

    flowLayout.minimumInteritemSpacing = 20 * KscreenWidth;

    //设置最小行间距

    flowLayout.minimumLineSpacing=20*KscreenHeight;

    //设置滚动方向

    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 52 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight - NAVIGATION_BAR_HEIGHT - 152 * KscreenHeight) collectionViewLayout:flowLayout];

    _collectionView.backgroundColor = [UIColor whiteColor];

    //设置代理

    _collectionView.delegate = self;

    //设置数据源

    _collectionView.dataSource = self;

    //注册

    [_collectionView registerClass:[ShaiXuanCell class] forCellWithReuseIdentifier:@"ShaiXuanCell"];

    //添加到视图

    [self.view addSubview:_collectionView];

}

#pragma mark 必须实现的方法

//每分区有多少个item

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

    return _dataArray.count;

}

//每个item上面现实的内容

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

//    从重用池里面获取cell

    ShaiXuanCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShaiXuanCell" forIndexPath:indexPath];

//    防止cell复用带来的控件重叠

//        for (UIView *view in cell.contentView.subviews) {

//            [view removeFromSuperview];

//        }

    cell.nameLable.text=_dataArray[indexPath.row];

    if(_selectedBtn.selected==YES) {

        cell.contentView.backgroundColor = hui1Color;

    }else{

        cell.contentView.backgroundColor = [UIColor whiteColor];

    }

    returncell;

}

//设置分区数

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

    return 1;

}

//点击item触发的事件

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

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    NSString*str =_dataArray[indexPath.row];

    NSLog(@"%@",str);

    if (_selectedArray.count > 0) {

        if([self.selectedArraycontainsObject:str]) {

            [self.selectedArrayremoveObject:str];

            cell.contentView.backgroundColor = [UIColor whiteColor];

            cell.selected= !cell.selected;

            return;

        }

        [self.selectedArrayaddObject:str];

        cell.contentView.backgroundColor = hui1Color;

    }else{

        [self.selectedArrayaddObject:str];

        cell.contentView.backgroundColor = hui1Color;

    }

}

//返回分区内边距的上左下右的距离

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

    return UIEdgeInsetsMake(30 * KscreenWidth, 30 *KscreenHeight, 30 * KscreenWidth, 30 * KscreenHeight);

}


cell中只用了一个lable,具体如下:

-(instancetype)initWithFrame:(CGRect)frame{

    if(self= [superinitWithFrame:frame]) {

        self.nameLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 90 * KscreenWidth, 30 * KscreenHeight)];

        self.nameLable.center = self.contentView.center;

        self.nameLable.textAlignment = NSTextAlignmentCenter;

        self.nameLable.font = [UIFont systemFontOfSize:15 * KscreenWidth];

        self.nameLable.textColor=ZhuTiColor;

        self.nameLable.text=@"经济";

        [self.contentView addSubview:self.nameLable];

        self.contentView.layer.masksToBounds = YES;

        self.contentView.layer.cornerRadius = 5 * KscreenWidth;

        self.contentView.layer.borderColor = ZhuTiColor.CGColor;

        self.contentView.layer.borderWidth = 0.5;

    }

    return self;

}

到此完活,因为按照需求写完之后从项目中抠出的一部分,所以有点乱,欢迎大家指正!勿喷,哈哈。。。

上一篇下一篇

猜你喜欢

热点阅读