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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM