Erlo

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

2019-04-22 18:02:20 发布   626 浏览  
页面报错/反馈
收藏 点赞

上篇对于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 JAVA JS 小程序 Python SEO MySql 确认