• UICollectionView设置首个cell默认选中(二)


    上篇对于UICollectionView默认选中cell采取的是每个cell分别对应一个标识,也就代表着废除了UICollectionView的重用机制。对于较少的数据情况是可以的,但是对于数据比较大,就会造成性能问题。

    于是思考在UICollectionView重用机制下,设置默认选中的cell,大致思路就是在cell被选中的时候设置一个selectIndexPath记录下来,在cell被取消选中的时候也用DeselectIndexPath记录下来,除了在cell被选中和取消选中的时候处理,还要在cell被赋值数据和cell即将出现的时候设置。

    在为CollectionView设置完数据之后,设置第0个cell被选中:

    #pragma mark 设置collectionView的数据
    - (void)setupCollectionViewData {
        
        for (int i = 0; i < 20; i++) {
            [self.dataArrayM addObject:[NSString stringWithFormat:@"第%d个cell",i]];
        }
        
        [self.testCollectionView reloadData];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        
        [self.testCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        [self collectionView:self.testCollectionView didSelectItemAtIndexPath:indexPath];
    }

    在viewDidLoad中为seleceIndex设置初试值,并在collectionView选中的方法中,赋值:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        
        [self setupUICollectionView];
        
        // 设置collectionView的数据
        [self setupCollectionViewData];
    }
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        
        self.selectIndexPath = indexPath;
        LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor greenColor]];
        [cell.nameLabel setTextColor:[UIColor redColor]];
    }

    在collectionView取消选中的代理方法中,为DeselectIndexPath赋值:

    - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
        self.DeselectIndexpath = indexPath;
        LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        if (cell == nil) { // 如果重用之后拿不到cell,就直接返回
            return;
        }
        [cell setBackgroundColor:[UIColor grayColor]];
        [cell.nameLabel setTextColor:[UIColor blackColor]];
    }

    在cell赋值的数据源方法中,设置cell的选中的样式:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        LBCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        [cell.nameLabel setText:self.dataArrayM[indexPath.row]];
        
        if ([self.selectIndexPath isEqual:indexPath]) {
            [cell setBackgroundColor:[UIColor greenColor]];
            [cell.nameLabel setTextColor:[UIColor redColor]];
        } else {
            [cell setBackgroundColor:[UIColor grayColor]];
            [cell.nameLabel setTextColor:[UIColor blackColor]];
        }
        
        
        
        return cell;
    }

    在cell出现正在展示的代理方法中再设置选中和未选中的样式:

    - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
        LBCollectionViewCell *LBcell = (LBCollectionViewCell *)cell;
        if (self.DeselectIndexpath && [self.DeselectIndexpath isEqual:indexPath]) {
            
            [LBcell setBackgroundColor:[UIColor grayColor]];
            [LBcell.nameLabel setTextColor:[UIColor blackColor]];
        }
        
        if ([self.selectIndexPath isEqual:indexPath]) {
            [LBcell setBackgroundColor:[UIColor greenColor]];
            [LBcell.nameLabel setTextColor:[UIColor redColor]];
        }
    }

    完整代码如下:

    //
    //  ViewController.m
    //  testSelect
    //
    //  Created by 李江波 on 2019/4/22.
    //  Copyright © 2019年 jinxiaofu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "LBCollectionViewCell.h"
    
    
    static NSString *const cellId = @"cellId";
    @interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
    // 数据数组
    @property (nonatomic, strong) NSMutableArray *dataArrayM;
    
    @property (nonatomic, weak) UICollectionView *testCollectionView;
    
    // 选中cell的indexPath
    @property (nonatomic, strong) NSIndexPath *selectIndexPath;
    
    // 取消选中的cell,防止由于重用,在取消选中的代理方法中没有设置
    @property (nonatomic, strong) NSIndexPath *DeselectIndexpath;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        
        [self setupUICollectionView];
        
        // 设置collectionView的数据
        [self setupCollectionViewData];
    }
    
    #pragma mark - private Method
    #pragma mark 设置collectionView的数据
    - (void)setupCollectionViewData {
        
        for (int i = 0; i < 20; i++) {
            [self.dataArrayM addObject:[NSString stringWithFormat:@"第%d个cell",i]];
        }
        
        [self.testCollectionView reloadData];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        
        [self.testCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        [self collectionView:self.testCollectionView didSelectItemAtIndexPath:indexPath];
    }
    
    #pragma mark - setupUI
    #pragma mark setupUICollectionView
    - (void)setupUICollectionView {
        // 设置uicollectionView样式
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 15;
        flowLayout.minimumInteritemSpacing = 15;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *testCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        [testCollectionView registerClass:[LBCollectionViewCell class] forCellWithReuseIdentifier:cellId];
        testCollectionView.delegate = self;
        testCollectionView.dataSource = self;
        [testCollectionView setBackgroundColor:[UIColor whiteColor]];
        [self.view addSubview:testCollectionView];
        self.testCollectionView = testCollectionView;
    }
    
    #pragma mark - UICollectionViewDatasource
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return 1;
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return [self.dataArrayM count];
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        LBCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        [cell.nameLabel setText:self.dataArrayM[indexPath.row]];
        
        if ([self.selectIndexPath isEqual:indexPath]) {
            [cell setBackgroundColor:[UIColor greenColor]];
            [cell.nameLabel setTextColor:[UIColor redColor]];
        } else {
            [cell setBackgroundColor:[UIColor grayColor]];
            [cell.nameLabel setTextColor:[UIColor blackColor]];
        }
        
        
        
        return cell;
    }
    
    #pragma mark - UICollectionViewDelegate
    - (CGSize) collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        return CGSizeMake(150, 150);
    }
    
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        
        self.selectIndexPath = indexPath;
        LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor greenColor]];
        [cell.nameLabel setTextColor:[UIColor redColor]];
    }
    
    - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
        self.DeselectIndexpath = indexPath;
        LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        if (cell == nil) { // 如果重用之后拿不到cell,就直接返回
            return;
        }
        [cell setBackgroundColor:[UIColor grayColor]];
        [cell.nameLabel setTextColor:[UIColor blackColor]];
    }
    
    
    - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
        LBCollectionViewCell *LBcell = (LBCollectionViewCell *)cell;
        if (self.DeselectIndexpath && [self.DeselectIndexpath isEqual:indexPath]) {
            
            [LBcell setBackgroundColor:[UIColor grayColor]];
            [LBcell.nameLabel setTextColor:[UIColor blackColor]];
        }
        
        if ([self.selectIndexPath isEqual:indexPath]) {
            [LBcell setBackgroundColor:[UIColor greenColor]];
            [LBcell.nameLabel setTextColor:[UIColor redColor]];
        }
    }
    
    
    #pragma mark - 懒加载
    - (NSMutableArray *)dataArrayM {
        if (!_dataArrayM) {
            _dataArrayM = [NSMutableArray array];
        }
        return _dataArrayM;
    }
    
    @end

     github地址: https://github.com/OLeGeB/selectCollectionViewCell.git

  • 相关阅读:
    PHP 数组函数分类整理
    mysql 数据库中 int(3) 和 int(11) 有区别么???
    理解PHP的运行机制
    类与对象
    PHP函数补完:call_user_func()
    linux 软件安装目录详解
    Linux下安装Redis
    Redis简介,应用场景,优势
    idea自动抽取变量快捷键设置
    全局捕获异常(适用于SpringMvc,SpringBoot项目)
  • 原文地址:https://www.cnblogs.com/muzichenyu/p/10750736.html
Copyright © 2020-2023  润新知