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
  • 相关阅读:
    Jmater (十七) 命令行(非GUI)模式详解(二) 执行代理设置
    Jmater (十七) 命令行(非GUI)模式详解(一) 执行、输出结果及日志、简单分步执行脚本
    Jmeter (十六) IF控制器
    Jmeter (十五)用户定义变量&用户参数
    JMeter (十四) Cookie & Session
    Jmeter (十三)调试工具之--HTTP Mirror Server(转载)
    Jmeter (十二)调试工具之--Debug Processor(转载)
    Jmeter (十一)调试工具之--Debug Sampler(转载)
    Jmeter (十)脚本增强_关联
    Shell 变量的截取
  • 原文地址:https://www.cnblogs.com/lsfv/p/9479267.html
Copyright © 2011-2022 走看看