zoukankan      html  css  js  c++  java
  • collectionview ,单选。多选。单选时,可单点取消选择

    //
    //  LSPopupcvVC.m
    //  MyCityFun
    //
    //  Created by liangshun on 18/8/7.
    //  Copyright © 2018年 liangshun. All rights reserved.
    //
    
    #import "LSFUNPopupcvVC.h"
    #import "LSPopcvCell.h"
    
    @interface LSFUNPopupcvVC ()
    
    @end
    
    @implementation LSFUNPopupcvVC
    
    -(id)initWithP:(NSMutableArray<NSString*>*)data preselect:(NSMutableArray<NSIndexPath*>*)preselect  handle:(UIViewController<IcvClick> *)handle mulsel:(BOOL)mulsel
    {
        if(self=[super init])
        {
            self.data=data;
            self.preselect=preselect;
            self.myhandle=handle;
            self.mulsel=mulsel;
        }
        return self;
    }
    
    -(void)loadView
    {
        self.view=self.myview=[[LSUIPopupcvView alloc]initWithP_MS:_mulsel];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self SetupCV];
    }
    
    
    -(void)SetupCV
    {
        _myview.cv_collection.dataSource=self;
        _myview.cv_collection.delegate=self;
        _myview.cv_collection.backgroundColor=[UIColor whiteColor];
        [self.myview.cv_collection registerClass:[LSPopcvCell class] forCellWithReuseIdentifier:@"cellId"];
        [_myview.view_main.btn_cancel addTarget:self action:@selector(OnClickCancel:) forControlEvents:UIControlEventTouchUpInside];
        [_myview.view_main.btn_ok addTarget:self action:@selector(OnClickOk:) forControlEvents:UIControlEventTouchUpInside];
        
        for(int i=0;i<self.preselect.count;i++)
        {
            [_myview.cv_collection selectItemAtIndexPath:_preselect[i] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        }
    }
    
    
    -(void)OnClickCancel:(UIButton*)sender
    {
        [_myhandle dismissViewControllerAnimated:NO completion:nil];
    }
    
    
    -(void)OnClickOk:(UIButton*)sender
    {
        [_myhandle dismissViewControllerAnimated:NO completion:^{
            [_myhandle OnCVClickOK:(NSMutableArray<NSIndexPath*>*)[_myview.cv_collection indexPathsForSelectedItems] sender:self];
        }];
    }
    
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return self.data.count;
    }
    
    -(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        LSPopcvCell *cell = (LSPopcvCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
        
        cell.botlabel.text=self.data[indexPath.row];
        cell.botlabel.tag=indexPath.row;
        
        if(cell.selected)
        {
            cell.botlabel.backgroundColor=[UIColor blueColor];
            cell.botlabel.textColor=[UIColor whiteColor];
        }
        else
        {
            cell.botlabel.backgroundColor=[UIColor whiteColor];
            cell.botlabel.textColor=[UIColor blackColor];
        }
        
        return (UICollectionViewCell*)cell;
    }
    
    -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        if(_mulsel==false)
        {
           if( [collectionView cellForItemAtIndexPath:indexPath].selected)
           {
               [_myview.cv_collection deselectItemAtIndexPath:indexPath animated:NO];
               [self collectionView:_myview.cv_collection didDeselectItemAtIndexPath:indexPath];
                return false;
           }
        }
        return true;
    }
    
    //auto fire event:choose
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"did%i,%i",(int)indexPath.row,(int)collectionView.visibleCells.count);
        ( (LSPopcvCell *)[collectionView cellForItemAtIndexPath:indexPath]).botlabel.backgroundColor=[UIColor blueColor];
        ( (LSPopcvCell *)[collectionView cellForItemAtIndexPath:indexPath]).botlabel.textColor=[UIColor whiteColor];
    }
    
    //auto fire event:preselect and then choose other.
    - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(nonnull NSIndexPath *)indexPath
    {
        NSLog(@"undid%i,%i",(int)indexPath.row,(int)collectionView.visibleCells.count);
        ( (LSPopcvCell *)[collectionView cellForItemAtIndexPath:indexPath]).botlabel.backgroundColor=[UIColor whiteColor];
        ( (LSPopcvCell *)[collectionView cellForItemAtIndexPath:indexPath]).botlabel.textColor=[UIColor blackColor];
    }
    
    
    
    @end
  • 相关阅读:
    JS身份证真实性校验(一)
    Python之文件操作
    python之数据类型
    Python之循环条件、变量、字符串格式化
    webpack之proxyTable设置跨域
    vue报错解决方案
    CentOS 7 下Ansiable搭建命令列表 及常用监控指令
    CentOS 7 下nagios搭建记录
    弹窗鼠标拖动功能-js
    做好探索性测试,体现你的价值
  • 原文地址:https://www.cnblogs.com/lsfv/p/9479267.html
Copyright © 2011-2022 走看看